在数据框中的特定位置添加新行 [英] Add a new row in specific place in a dataframe

查看:151
本文介绍了在数据框中的特定位置添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有我的数据:

 > data
  Manufacturers       Models
1   Audi                RS5  
2   BMW                 M3  
3   Cadillac            CTS-V  
4   Lexus               ISF

我想在第四行中添加1行,例如:

I would like to add 1 row in the fourth row, like this:

 > data
  Manufacturers       Models
1   Audi                RS5  
2   BMW                 M3  
3   Cadillac            CTS-V  
4   Benz                C63
5   Lexus               ISF

我试图像这样使用rbind():

I have tried to use the rbind() like this:

 Benz = data.frame(Manufacturers = "Benz", Models = "C63")
 newdata = rbind(data,Benz)

但是我无法添加到我想要的地方。
我希望在这个问题上有任何帮助。非常感谢。

But I cannot add to the place I want. I would appreciate any help on this question. Thanks a lot.

推荐答案

如果您不想要索引,而只是一次一次性的快速修复类似于电子表格的外观,您可以求助于

In case you don't want the index but rather a one-off "quick fix" for some spreadsheet-like appearance, you might resort to

newData <- rbind( data[1:3,], Benz, data[ 4,] )

这篇关于在数据框中的特定位置添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆