[R] Make a column from columns

Jim Lemon jim at bitwrit.com.au
Thu Jan 3 11:17:00 CET 2013


On 01/03/2013 09:07 PM, Jim Lemon wrote:
> On 01/03/2013 04:00 PM, Aseem Sharma wrote:
>> Hi,
>> I have a data matrix with 13 columns and 55 rows. First coulmn is year
>> and
>> other are monthly values for 55 years.
>> Now i want to create a single column of all the values ( i e a single
>> time
>> series column from column 2 to 13 ).
>> Is it the for loop that works here ...how ?
>> or how i can rearrange the data so that it becomes a times series column.
>> hope i made the question clear .
>> thank you,
>>
>>
> Hi Aseem,
> This is a "reshape" problem. There is a "reshape" function in the stats
> package that comes with R that will do this. Say your data frame is
> named "mydata" and the columns are named "year" and "Jan" to "Dec":
>
> reshape(mydata,varying=month.abb,v.names="month",
> timevar="year",direction="long")
>
Or, now that I think of it:

library(prettyR)
rep_n_stack(mydata,to.stack=month.abb,stack.names=c("month","value"))

Jim




More information about the R-help mailing list