[R] transform variables
arun
smartpink111 at yahoo.com
Mon Aug 26 15:08:28 CEST 2013
HI,
You could also try:
res<-reshape(yrmon,varying=!grepl("year",colnames(yrmon)),v.names="Values",timevar="Month",direction="long")[,-4]
res1<- res[order(res$year,res$Month),]
head(res1)
# year Month Values
#1.1 1901 1 -0.2557446
#1.2 1901 2 -0.2318646
#1.3 1901 3 -0.1961822
#1.4 1901 4 0.5609812
#1.5 1901 5 0.5713866
#1.6 1901 6 -1.5676166
A.K.
----- Original Message -----
From: Jim Lemon <jim at bitwrit.com.au>
To: catalin roibu <catalinroibu at gmail.com>
Cc: r-help at r-project.org
Sent: Monday, August 26, 2013 7:45 AM
Subject: Re: [R] transform variables
On 08/26/2013 09:04 PM, catalin roibu wrote:
> Dear all!
>
> I have a data frame composed by 13 columns (year, and 12 months). I want to
> transform this data base in another like this
> year month values
> 1901 1
> 1901 2
> 1901 3
> .....
> 1901 12
> 1902 1
> 1902 2
> ....
> 1902 12
>
> Is there a possibility to succeed that in R?
>
> Thank you!
>
> best regards!
> CR
>
Hi catalin,
Try this:
yrmon<-data.frame(cbind(1901:1905,matrix(rnorm(60),ncol=12)))
names(yrmon)<-c("year",month.abb)
library(prettyR)
yrmon2<-rep_n_stack(yrmon,to.stack=2:13)
yrmon2[order(yrmon2$year),]
Jim
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list