Dear R-community, When doing this:> test<-data.frame(a=c(1,2,3)) > rbind(test$a, 3)I expect something like:> 1 > 2 > 3 > 2but get:> [,1] [,2] [,3] >[1,] 1 2 3 >[2,] 2 2 2the same for: rbind(test[["a"]], 2) or rbind(as.vector(test[["a"]]), 2) or rbind(t(as.vector(test[["a"]])), 2) Why is that and how do I extract the "values" from a dataframe to get the desired result? Thank you Dom -- View this message in context: http://r.789695.n4.nabble.com/Append-data-to-vector-form-a-column-of-a-dataframe-tp3897205p3897205.html Sent from the R help mailing list archive at Nabble.com.
Ivan Calandra
2011-Oct-12 10:19 UTC
[R] Append data to vector form a column of a dataframe
Hi Dom, This is because "3" is recycled. It is necessary because the number of columns have to be the same for every row. Try this instead: c(test$a, 2) ## you wrote "3" but meant "2" I guess HTH, Ivan Le 10/12/2011 10:47, behave a ?crit :> Dear R-community, > > When doing this: > >> test<-data.frame(a=c(1,2,3)) >> rbind(test$a, 3) > I expect something like: > >> 1 >> 2 >> 3 >> 2 > but get: >> [,1] [,2] [,3] >> [1,] 1 2 3 >> [2,] 2 2 2 > > the same for: > > rbind(test[["a"]], 2) > or > rbind(as.vector(test[["a"]]), 2) > or > rbind(t(as.vector(test[["a"]])), 2) > > Why is that and how do I extract the "values" from a dataframe to get the > desired result? > > Thank you > Dom > > > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Append-data-to-vector-form-a-column-of-a-dataframe-tp3897205p3897205.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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Dept. Mammalogy Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php
In addition to Ivan, test$a is not a data.frame anymore but a numerical vector.> class(test$a)[1] "numeric"> test$a[1] 1 2 3 So adding a row to your data.frame would be?> rbind(test, 2)? a 1 1 2 2 3 3 4 2 ? Wolfgang Wu ----- Urspr?ngliche Message ----- Von: Ivan Calandra <ivan.calandra at uni-hamburg.de> An: r-help at r-project.org Cc: Gesendet: 11:19 Mittwoch, 12.Oktober 2011 Betreff: Re: [R] Append data to vector form a column of a dataframe Hi Dom, This is because "3" is recycled. It is necessary because the number of columns have to be the same for every row. Try this instead: c(test$a, 2)? ## you wrote "3" but meant "2" I guess HTH, Ivan Le 10/12/2011 10:47, behave a ?crit :> Dear R-community, > > When doing this: > >> test<-data.frame(a=c(1,2,3)) >> rbind(test$a, 3) > I expect something like: > >> 1 >> 2 >> 3 >> 2 > but get: >>? ? ? [,1] [,2] [,3] >> [1,]? ? 1? ? 2? ? 3 >> [2,]? ? 2? ? 2? ? 2 > > the same for: > > rbind(test[["a"]], 2) > or > rbind(as.vector(test[["a"]]), 2) > or > rbind(t(as.vector(test[["a"]])), 2) > > Why is that and how do I extract the "values" from a dataframe to get the > desired result? > > Thank you > Dom > > > > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Append-data-to-vector-form-a-column-of-a-dataframe-tp3897205p3897205.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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Dept. Mammalogy Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php ______________________________________________ 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.