[R] transform variables
arun
smartpink111 at yahoo.com
Mon Aug 26 16:02:17 CEST 2013
Hi,
Also, you could also try:
vec1<-rep(yrmon[,1],each=(ncol(yrmon)-1))
vec2<-as.vector(t(yrmon[,-1]))
vec3<- rep(1:12,nrow(yrmon))
res2<-data.frame(year=vec1,Month=vec3,Values=vec2)
row.names(res1)<- row.names(res2)
attr(res1,"row.names")<- attr(res2,"row.names")
identical(res1,res2)
#[1] TRUE
#Speed comparison
indx<-rep(1:nrow(yrmon),6e4)
yrmon1<- yrmon[indx,]
dim(yrmon1)
#[1] 300000 13
system.time({res<-reshape(yrmon1,varying=!grepl("year",colnames(yrmon)),v.names="Values",timevar="Month",direction="long")[,-4]
res1<- res[order(res$year,res$Month),]})
# user system elapsed
# 22.540 0.208 22.790
system.time({vec1<-rep(yrmon1[,1],each=(ncol(yrmon1)-1))
vec2<-as.vector(t(yrmon1[,-1]))
vec3<- rep(1:12,nrow(yrmon1))
res2<-data.frame(year=vec1,Month=vec3,Values=vec2)
res3<- res2[order(res2$year,res2$Month),] ###If ordering is not important, this will further increase the speed
})
# user system elapsed
# 6.856 0.044 6.912
row.names(res1)<- 1:nrow(res1)
row.names(res3)<- row.names(res1)
attr(res1,"row.names")<- attr(res3,"row.names")
identical(res1,res3)
#[1] TRUE
library(prettyR)
system.time({
yrmon2<-rep_n_stack(yrmon1,to.stack=2:13)
yrmon3<-yrmon2[order(yrmon2$year),]})
# user system elapsed
# 9.372 0.084 9.475
yrmon3$group1<- as.integer(as.numeric(factor(yrmon3$group1,levels=month.abb)))
colnames(yrmon3)<- colnames(res3)
row.names(yrmon3)<- row.names(res3)
identical(res3,yrmon3)
#[1] TRUE
A.K.
----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: catalin roibu <catalinroibu at gmail.com>
Cc: Jim Lemon <jim at bitwrit.com.au>; R help <r-help at r-project.org>
Sent: Monday, August 26, 2013 9:08 AM
Subject: Re: [R] transform variables
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