[R] Is there way to add a new row to a data frame in a specific location

David Winsemius dwinsemius at comcast.net
Fri Nov 25 20:16:35 CET 2011


On Nov 25, 2011, at 2:10 PM, Ian Strang wrote:

> This look really interesting but I don't understand what is happening.
> Please can someone explain the last line and what the bit in [] is  
> doing.
> Ian

You just stick the new line on the bottom and return the rows in the  
order specified in the "i" argument to "[". It's just like vector  
indexing except with rows.

 > (1:4)[c(4,2,3,1)]
[1] 4 2 3 1
 > (4:1)[c(4,2,3,1)]
[1] 1 3 2 4
 >

>
> df = data.frame( A=c('a','b','c'), B=c(1,2,3), C=c(10,20,30),
> stringsAsFactors=FALSE)
>
> newrow = c('X', 100, 200)
>
> rbind(df,newrow)[c(1,4,2,3),]
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list