[R] Copy dataframe for another

Petr PIKAL petr.pikal at precheza.cz
Fri Mar 9 14:43:52 CET 2012


Hi

> 
> Hello,
> 
>     the idea is to copy the d for df, with new results.
> 
> x<-data.frame(name="x1",pos=4,age=20)
> x<-rbind(x,data.frame(name="x2",pos=5,age=20))
> x<-rbind(x,data.frame(name="x3",pos=6,age=21))
> x<-rbind(x,data.frame(name="x4",pos=7,age=24))
> x<-rbind(x,data.frame(name="x5",pos=8,age=27))
> x<-rbind(x,data.frame(name="x6",pos=9,age=26))
> View(x)
> 
> 
> d<-NULL
> df<-NULL
> 
> for(r in 2: nrow(x))
> {
>   val_user<-x$name[[r]]

x$name[r]
is enough
x$name shall be plain vector


>   pos<-x$pos[[r]] -4
>   age <-x$age[[r]]
>   d<-data.frame(val_user,pos,age)
>   print(d)
> }
> df<-rbind(df,d)
> View(df)
> 
>    in df only have the last result, the ideia is have the new results
> calculated in the for loop

It is not very efficient way of doing things in R, but if you insist.

d is overwritten in each iteration therefore only last value is stored. 
You need to put values from each cycle to separate slot. So you shall put 

df<-rbind(df,d)

into the cycle.

As I said it is not very convenient and effective way, but if you want to 
shoot yourself to your leg....

Regards
Petr


> 
> thanks
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Copy-
> dataframe-for-another-tp4456893p4459068.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> 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