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 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. 


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 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

	[[alternative HTML version deleted]]

