如何在R中的数据框中添加一行? [英] How can a add a row to a data frame in R?

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

问题描述

在R中,一旦数据帧已经初始化,如何在数据帧中添加新行?

In R, how do you add a new row to a data frame once the data frame has already been initialized?

到目前为止,我有这个内容:

So far I have this:

df <- data.frame("hi", "bye")
names(df) <- c("hello", "goodbye")

#I am trying to add "hola" and "ciao" as a new row
de <- data.frame("hola", "ciao")

merge(df, de) # Adds to the same row as new columns

# Unfortunately, I couldn't find an rbind() solution that wouldn't give me an error

任何帮助将不胜感激

推荐答案

就像@Khashaa和@Richard Scriven在注释中指出的那样,您必须为要附加的所有数据帧设置一致的列名。

Like @Khashaa and @Richard Scriven point out in comments, you have to set consistent column names for all the data frames you want to append.

因此,您需要显式声明第二个数据框的列名称 de ,然后使用 rbind()。您只需设置第一个数据框的列名称 df

Hence, you need to explicitly declare the columns names for the second data frame, de, then use rbind(). You only set column names for the first data frame, df:

df<-data.frame("hi","bye")
names(df)<-c("hello","goodbye")

de<-data.frame("hola","ciao")
names(de)<-c("hello","goodbye")

newdf <- rbind(df, de)

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

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