将数据合并到现有列中

时间:2016-05-22 19:21:44

标签: r dataframe merge

我正在尝试将数据框中的数据合并为一个由&&分隔的值,使用merge(无特殊原因)。有人可以用这个命令解释我错过了什么吗?

news<-data.frame(c("2016-05-20","2016-05-19","2016-05-19"),c("x","y","z"))
data<-data.frame(c("2016-05-20","2016-05-21","2016-05-22"),c(1,2,3))

#bind news with the same date into value seperated by &&
    news<-merge(news,by.x=news[,1])
    #Error in as.data.frame(y) : argument "y" is missing, with no default

奖金问题:

#merge news with data based on matching date
    merge(news,data,by.x=news[,1],by.y=data[,1])
    #Error in fix.by(by.x, x) : 'by' must specify uniquely valid columns

目标:

1                                  2016-05-20          1      x
2                                  2016-05-19          NA     y && z
3                                  2016-05-21          2      NA
4                                  2016-05-22          3      NA

2 个答案:

答案 0 :(得分:4)

这会产生您想要的输出,但这是一个两步过程。

# get data with some nice names
news <- data.frame(date=c("2016-05-20","2016-05-19","2016-05-19"), lets=c("x","y","z"))
data <- data.frame(date=c("2016-05-20","2016-05-21","2016-05-22"), nums=c(1,2,3))

# combine observations with the same date
newsC <- aggregate(lets~date, data=news, FUN=paste, collapse="&&")
merge(data, newsC, by="date", all=TRUE)

您遇到的第一个错误是因为您没有在merge中指定第二个data.frame。

答案 1 :(得分:1)

基于plyr / dplyr的解决方案:

library(dplyr)

news <- data.frame(date=c("2016-05-20","2016-05-19","2016-05-19"),
                   letters=c("x","y","z"), stringsAsFactors = FALSE)
data <- data.frame(date=c("2016-05-20","2016-05-21","2016-05-22"),
                   numbers=c(1,2,3), stringsAsFactors = FALSE)

df <- plyr::rbind.fill(news,data)

df.combined <- df %>% group_by(date) %>% summarize_each(funs(paste(na.omit(.), collapse=" && ")), letters:numbers)