r - How do I create a dataframe from a colsum function -


i have dataframe trying sum each column given condition. using colsum function. based on result create data frame.

see code

> colsums(product.trx[5:10])  2011-04 2011-05 2011-06 2011-07 2011-08 2011-09 2011-10 2011-11 2011-12         0       0   23318   21187   23933   22911   22600   23053   23467     

based on these results create dataframe months(eg. 2010-11) column name , corresponding values. going add new column called "product" , value 'aaaaaa' dataframe should this

product 2011-04 2011-05 2011-06 2011-07 2011-08 2011-09 2011-10 2011-11 2011-12   aaaaaa      0       0   23318   21187   23933   22911   22600   23053   23467    

how code in r?

your appreciated.

assuming data numeric matrix column names, this:

> m = matrix(1:12,3,4) > colnames(m)=c("a","b","c","d") 

then this:

> data.frame(product="aaaa",t(colsums(m)))   product  b  c  d 1    aaaa 6 15 24 33 

if thing data frame, still works:

> md=data.frame(m) > md$d = runif(3) # mix floats > data.frame(product="aaaa",t(colsums(md)))   product  b  c        d 1    aaaa 6 15 24 1.285573 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -