在 R 中,一旦数据框已经初始化,如何向数据框添加新行?
到目前为止,我有这个:
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 在评论中指出的那样,您必须为要附加的所有数据框设置一致的列名。
因此,您需要显式声明第二个数据框的列名de,然后使用rbind(). 您只需为第一个数据框设置列名,df:
de
rbind()
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)