Dear all, I have a question concerning manipulating data of several columns of a dataframe at the same time. I manage to do it for one column (with the use of the specific name for this column). In each columns, I have 60 values. But I should reorganize the values (because I created this as an output before and I want to compare it with an other dataset). I want that the value on row 2 becomes the value of row 1, value 3 value 2 and so on. The first value would be NA. If I would do this for 1 column (with the name depth_1), I would do it like this: for (t in 2:60) { results$depth[t]<-new$depth_1[t-1] } # But in my dataset I have 91 columns and I would like to find a way not having to write this for every column? # I cannot give my dataset where I?m working on so I created one just for trying it out and to provide a reproducible example. I created a data frame ?new? with 26 columns and 60 rows. I named the columns all in a similar way using ?C <- seq(1,13.5,0.5)?. That means that all my column names are structured in the same way: depth_1 ; depth_1.5, depth_2; depth_2.5 and so on. C <- seq(1,13.5,0.5) a<-c(1:60) b<-c(2:61) c<-c(3:62) d<-c(1:60) e<-c(2:61) f<-c(3:62) g<-c(1:60) h<-c(2:61) i<-c(3:62) j<-c(1:60) k<-c(2:61) l<-c(3:62) m<-c(1:60) n<-c(2:61) o<-c(3:62) p<-c(1:60) q<-c(2:61) r<-c(3:62) s<-c(1:60) t<-c(2:61) u<-c(3:62) v<-c(1:60) w<-c(2:61) x<-c(3:62) y<-c(1:60) z<-c(2:61) new<-data.frame(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) names(new)<-c(paste('depth_',C,sep='')) #I don't know if I may create a now dataframe that's empty and then binding my results to it? Or should I create a dataframe with already one column with the amount of rows that I will have in my new dataset? #results <- data.frame() is what I would use to create an empty dataframe but that gives some problems when I want to add other columns because the length is different. results <- data.frame(time=1:60) ### I use n in 2:27 because this needs to be done on column 2 till column 27, not on column 1 for (n in 2:27) { # I don?t know if I should indicate that there should be created an new variable with 60 rows? I saw people doing it in a script, but it didn?t seem necessary in an other script that was similar to it and I made myself? # results$newdepth<-(1:60) #next line I also don?t know if I should give it. If I did this whole thing for only 1 column, the firs row was NA without asking for it. results$newdepth[1,n]<-NA for (t in 2:60) { results$newdepth[t,n] <- new[t-1,n] } results<- cbind(results, results$newdepth) } names(results) <- c(names(results)[c(1)],paste('newdepth_',C,sep='')) #for example: results$newdepth_1.5 I hope someone can help me with this? I hope I gave enough information? I think there should be an easier manner but I really have no other idea. I?m also wondering if it?s possible not using column numbers in a function, but the name if those are structured in the same way? Eg. depth_?followed by a number? If you want for example to do something with the columns depth_1, depth_1.5, depth_2, depth_2.5, depth_3 ? belonging to a dataframe with also other columns not related with the name depth (Like here, there is also the column time). I?m hoping there is something useful for that. Many thanks, Nerak -- View this message in context: http://r.789695.n4.nabble.com/manipulating-data-of-several-columns-simultaneously-tp4306278p4306278.html Sent from the R help mailing list archive at Nabble.com.
Petr Savicky
2012-Jan-18 12:58 UTC
[R] manipulating data of several columns simultaneously
On Wed, Jan 18, 2012 at 02:48:48AM -0800, Nerak wrote:> Dear all, > I have a question concerning manipulating data of several columns of a > dataframe at the same time. > I manage to do it for one column (with the use of the specific name for this > column). > In each columns, I have 60 values. But I should reorganize the values > (because I created this as an output before and I want to compare it with an > other dataset). I want that the value on row 2 becomes the value of row 1, > value 3 value 2 and so on. The first value would be NA. > > If I would do this for 1 column (with the name depth_1), I would do it like > this: > > for (t in 2:60) > { > results$depth[t]<-new$depth_1[t-1] > } > > > # But in my dataset I have 91 columns and I would like to find a way not > having to write this for every column? > # I cannot give my dataset where I?m working on so I created one just for > trying it out and to provide a reproducible example. I created a data frame > ?new? with 26 columns and 60 rows. I named the columns all in a similar way > using ?C <- seq(1,13.5,0.5)?. That means that all my column names are > structured in the same way: depth_1 ; depth_1.5, depth_2; depth_2.5 and so > on. > > C <- seq(1,13.5,0.5) > > a<-c(1:60) > b<-c(2:61) > c<-c(3:62) > d<-c(1:60) > e<-c(2:61) > f<-c(3:62) > g<-c(1:60) > h<-c(2:61) > i<-c(3:62) > j<-c(1:60) > k<-c(2:61) > l<-c(3:62) > m<-c(1:60) > n<-c(2:61) > o<-c(3:62) > p<-c(1:60) > q<-c(2:61) > r<-c(3:62) > s<-c(1:60) > t<-c(2:61) > u<-c(3:62) > v<-c(1:60) > w<-c(2:61) > x<-c(3:62) > y<-c(1:60) > z<-c(2:61) > > new<-data.frame(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) > names(new)<-c(paste('depth_',C,sep='')) >If you want to keep the vector of row names unchanged, try the following. shift <- function(x) { c(NA, x[1:(length(x)-1)]) } new1 <- as.data.frame(lapply(new, shift)) new[c(1:5, 57:60), 1:5] depth_1 depth_1.5 depth_2 depth_2.5 depth_3 1 1 2 3 1 2 2 2 3 4 2 3 3 3 4 5 3 4 4 4 5 6 4 5 5 5 6 7 5 6 57 57 58 59 57 58 58 58 59 60 58 59 59 59 60 61 59 60 60 60 61 62 60 61 new1[c(1:5, 57:60), 1:5] depth_1 depth_1.5 depth_2 depth_2.5 depth_3 1 NA NA NA NA NA 2 1 2 3 1 2 3 2 3 4 2 3 4 3 4 5 3 4 5 4 5 6 4 5 57 56 57 58 56 57 58 57 58 59 57 58 59 58 59 60 58 59 60 59 60 61 59 60 Petr Savicky.
Gerrit Eichner
2012-Jan-18 13:11 UTC
[R] manipulating data of several columns simultaneously
Hello, Nerak, maybe rbind( NA, head( results, -1)) does what you want (for all columns at once)? Hth, Gerrit On Wed, 18 Jan 2012, Nerak wrote:> Dear all, > I have a question concerning manipulating data of several columns of a > dataframe at the same time. I manage to do it for one column (with the > use of the specific name for this column). In each columns, I have 60 > values. But I should reorganize the values (because I created this as an > output before and I want to compare it with an other dataset). I want > that the value on row 2 becomes the value of row 1, value 3 value 2 and > so on. The first value would be NA. > > If I would do this for 1 column (with the name depth_1), I would do it > like this: > > for (t in 2:60) > { > results$depth[t]<-new$depth_1[t-1] > }[ ... snip ... ]
Jean V Adams
2012-Jan-18 13:11 UTC
[R] manipulating data of several columns simultaneously
Nerakg wrote on 01/18/2012 04:48:48 AM:> Dear all, > I have a question concerning manipulating data of several columns of a > dataframe at the same time. > I manage to do it for one column (with the use of the specific name forthis> column). > In each columns, I have 60 values. But I should reorganize the values > (because I created this as an output before and I want to compare itwith an> other dataset). I want that the value on row 2 becomes the value of row1,> value 3 value 2 and so on. The first value would be NA.Try this. # Combine one row of NAs, with the original data frame, minus the last row results <- rbind(rep(NA, dim(new)[2]), new[-dim(new)[1], ]) new[c(1:3, 58:60), 1:6] depth_1 depth_1.5 depth_2 depth_2.5 depth_3 depth_3.5 1 1 2 3 1 2 3 2 2 3 4 2 3 4 3 3 4 5 3 4 5 58 58 59 60 58 59 60 59 59 60 61 59 60 61 60 60 61 62 60 61 62 results[c(1:3, 58:60), 1:6] depth_1 depth_1.5 depth_2 depth_2.5 depth_3 depth_3.5 1 NA NA NA NA NA NA 2 1 2 3 1 2 3 3 2 3 4 2 3 4 58 57 58 59 57 58 59 59 58 59 60 58 59 60 60 59 60 61 59 60 61 Jean> If I would do this for 1 column (with the name depth_1), I would do itlike> this: > > for (t in 2:60) > { > results$depth[t]<-new$depth_1[t-1] > } > > > # But in my dataset I have 91 columns and I would like to find a way not > having to write this for every column? > # I cannot give my dataset where I?m working on so I created one justfor> trying it out and to provide a reproducible example. I created a dataframe> ?new? with 26 columns and 60 rows. I named the columns all in a similarway> using ?C <- seq(1,13.5,0.5)?. That means that all my column names are > structured in the same way: depth_1 ; depth_1.5, depth_2; depth_2.5 andso> on. > > C <- seq(1,13.5,0.5) > > a<-c(1:60) > b<-c(2:61) > c<-c(3:62) > d<-c(1:60) > e<-c(2:61) > f<-c(3:62) > g<-c(1:60) > h<-c(2:61) > i<-c(3:62) > j<-c(1:60) > k<-c(2:61) > l<-c(3:62) > m<-c(1:60) > n<-c(2:61) > o<-c(3:62) > p<-c(1:60) > q<-c(2:61) > r<-c(3:62) > s<-c(1:60) > t<-c(2:61) > u<-c(3:62) > v<-c(1:60) > w<-c(2:61) > x<-c(3:62) > y<-c(1:60) > z<-c(2:61) > > new<-data.frame(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) > names(new)<-c(paste('depth_',C,sep='')) > > #I don't know if I may create a now dataframe that's empty and thenbinding> my results to it? Or should I create a dataframe with already one column > with the amount of rows that I will have in my new dataset? #results <- > data.frame() is what I would use to create an empty > dataframe but that gives some problems when I want to add other columns > because the length is different. > results <- data.frame(time=1:60) > ### I use n in 2:27 because this needs to be done on column 2 tillcolumn> 27, not on column 1 > for (n in 2:27) > { > > # I don?t know if I should indicate that there should be created an new > variable with 60 rows? I saw people doing it in a script, but it didn?tseem> necessary in an other script that was similar to it and I made myself? > # results$newdepth<-(1:60) > #next line I also don?t know if I should give it. If I did this wholething> for only 1 column, the firs row was NA without asking for it. > > results$newdepth[1,n]<-NA > > for (t in 2:60) > { > results$newdepth[t,n] <- new[t-1,n] > } > results<- cbind(results, results$newdepth) > } > > names(results) <- c(names(results)[c(1)],paste('newdepth_',C,sep='')) > #for example: > results$newdepth_1.5 > > I hope someone can help me with this? I hope I gave enough information?I> think there should be an easier manner but I really have no other idea. > I?m also wondering if it?s possible not using column numbers in afunction,> but the name if those are structured in the same way? Eg.depth_?followed by> a number? If you want for example to do something with the columnsdepth_1,> depth_1.5, depth_2, depth_2.5, depth_3 ? belonging to a dataframe withalso> other columns not related with the name depth (Like here, there is alsothe> column time). I?m hoping there is something useful for that. > > Many thanks, > Nerak[[alternative HTML version deleted]]
Reasonably Related Threads
- Accomplishing a loop on multiple columns
- result numeric(0) when using variable1[which(variable2="max(variable2)"]
- nls: how do you know if the model is significant?
- different way for a for loop for several columns?
- Non-linear curve fitting (nls): starting point and quality of fit