如何在数据框R的顶部添加行 [英] How to add row on-top of data frame R

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

问题描述

我有以下数据框:

> dat

  V1   V2
1  1 6868
2  2  954
3  3   88

我想做的是在当前结果上方添加另一行:

What I want to do is to add another row on top of the current one resulting:

   V1   V2
1  0 10000
2  1 6868
3  2  954
4  3   88

为什么这不起作用:

new_dat <- rbind(dat,c(0,10000))

这样做的正确方法是什么?

What's the correct way to do it?

推荐答案

将所需的矢量首先放在顶部:

Put the vector you want on top first:

new_dat <- rbind(c(0,10000), dat)

但是,使用rbind这里假设所有列都是数字,并且假设列顺序与向量相同。通常,应将data.frames绑定在一起,如下所示,可以在其中根据需要混合列类型:

But, using rbind here assumes that all your columns are numeric, and you are assuming column orders the same as the vector. In general you should bind data.frames together, something like this, where you can mix column types if needed:

rbind(data.frame(V1 = 0, V2 = 10000), dat)

对于a像这样更一般的数据合并。

There are many other options for a more general merge of data like this.

这篇关于如何在数据框R的顶部添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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