I'm trying to copy the results of a dataframe to another within a cycle for but I am not able to implement the rbind, because give th d<-Null df<-NULL for(r in 2: nrow(x)) { val_user<-x.name[[r]] pos<-x.pos[[r]] -4 age <-x.age[[r]] d<-data.frame(val_user,pos,) print(d) } df<-rbind(df,d) } someone can help me solve this Thanks -- View this message in context: http://r.789695.n4.nabble.com/Copy-dataframe-for-another-tp4456879p4456879.html Sent from the R help mailing list archive at Nabble.com.
I'm trying to copy the results of a dataframe to another within a cycle for but I am not able to implement the rbind, because give th d<-Null df<-NULL for(r in 2: nrow(x)) { val_user<-x.name[[r]] pos<-x.pos[[r]] -4 age <-x.age[[r]] d<-data.frame(val_user,pos,) print(d) df<-rbind(df,d) } someone can help me solve this Thanks -- View this message in context: http://r.789695.n4.nabble.com/Copy-dataframe-for-another-tp4456886p4456886.html Sent from the R help mailing list archive at Nabble.com.
What goes wrong? Your code isn't reproducible and you didn't give an error message. I'd mention that "d <- Null" does not assign NULL to d which might lead to a dimension mismatch at some point. Also, something about your loop syntax seems funny: you are looping over rows but selecting columns of your data frame -- seems dangerous. Can you post a minimal working example including data (with dput()) ? Michael On Thu, Mar 8, 2012 at 11:26 AM, RMSOPS <ricardosousa2000 at clix.pt> wrote:> > I'm trying to copy the results of a dataframe to another within a cycle for > but I am not able to implement the rbind, because give th > > d<-Null > df<-NULL > > for(r in 2: nrow(x)) > ? ?{ > ? ? ?val_user<-x.name[[r]] > ? ? ?pos<-x.pos[[r]] -4 > ? ? ?age <-x.age[[r]] > ? ? ?d<-data.frame(val_user,pos,) > ? ? ?print(d) > ? ?} > ?df<-rbind(df,d) > } > > someone can help me solve this > > ? Thanks > > -- > View this message in context: http://r.789695.n4.nabble.com/Copy-dataframe-for-another-tp4456879p4456879.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.
What exactly are you trying to do? We can't replicate your code because we don't have your data, we don't know what your objective is, and we don't know what went wrong. Also, Null and NULL are not the same thing. On Thu, Mar 8, 2012 at 11:26 AM, RMSOPS <ricardosousa2000 at clix.pt> wrote:> > I'm trying to copy the results of a dataframe to another within a cycle for > but I am not able to implement the rbind, because give th > > d<-Null > df<-NULL > > for(r in 2: nrow(x)) > ? ?{ > ? ? ?val_user<-x.name[[r]] > ? ? ?pos<-x.pos[[r]] -4 > ? ? ?age <-x.age[[r]] > ? ? ?d<-data.frame(val_user,pos,) > ? ? ?print(d) > ? ?} > ?df<-rbind(df,d) > } > > someone can help me solve this > > ? Thanks >-- Sarah Goslee http://www.functionaldiversity.org
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]] 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 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.
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 loopIt 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hello Thanks for the reply. As yet I have not much experience in r, although I make some mistakes. Any tips to solve the problem of a more effective way. Regards -- View this message in context: http://r.789695.n4.nabble.com/Copy-dataframe-for-another-tp4456893p4459526.html Sent from the R help mailing list archive at Nabble.com.
On Mar 9, 2012, at 5:31 AM, RMSOPS wrote:> 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))x <- data.frame(name=paste("x", 1:6, sep=""), pos=3+1:6 age=c(20,20, 21,24,27,26)> View(x) > > > d<-NULL > df<-NULL > > for(r in 2: nrow(x)) > { > val_user<-x$name[[r]] > pos<-x$pos[[r]] -4 > age <-x$age[[r]] > d<-data.frame(val_user,pos,age) > print(d) > } > df<-rbind(df,d) > View(df) >It is generally better to spend more time describing the problem. Attempting to read the mind of new R users who come from other programming paradigms is often unrewarding. -- David.> in df only have the last result, the ideia is have the new results > calculated in the for loop > > 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.PLEASE: Include context. We are NOT reading this on Nabble. -- David Winsemius, MD West Hartford, CT
On Mar 9, 2012, at 10:41 AM, David Winsemius wrote:> > On Mar 9, 2012, at 5:31 AM, RMSOPS wrote: > >> 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))x <- data.frame(name=paste("x", 1:6, sep=""), pos=3+1:6, age=c(20,20, 21,24,27,26)) Added the missing comma and paren: Is this what you were attempting? > df <- x > df$pos[2:nrow(df)] <- df$pos[2:nrow(df)]-4 > names(df) <- c("val_user", "pos", "age") > df val_user pos age 1 x1 4 20 2 x2 1 20 3 x3 2 21 4 x4 3 24 5 x5 4 27 6 x6 5 26> > >> View(x) >> >> >> d<-NULL >> df<-NULL >> >> for(r in 2: nrow(x)) >> { >> val_user<-x$name[[r]] >> pos<-x$pos[[r]] -4 >> age <-x$age[[r]] >> d<-data.frame(val_user,pos,age) >> print(d) >> } >> df<-rbind(df,d) >> View(df) >> > > It is generally better to spend more time describing the problem. > Attempting to read the mind of new R users who come from other > programming paradigms is often unrewarding. > > -- > David. > >> in df only have the last result, the ideia is have the new results >> calculated in the for loop >> >> 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. > > PLEASE: Include context. We are NOT reading this on Nabble. > > --David Winsemius, MD West Hartford, CT