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

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

问题描述

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

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

到目前为止我有这个:

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天全站免登陆