如何合并多个数据框,添加新值并为缺失值分配0

时间:2018-11-05 05:22:05

标签: r merge

我在合并R中的多个数据帧时遇到问题,例如:

table1:
NAME A1
a 1
b 2
c 3

table2:
NAME A2
a 10
b 20
d 30

table3:
NAME A3
a 100
b 200
e 400

我想通过“ NAME”合并这些表,添加新值并将“ 0”分配给缺少的值,例如:

merged table:
NAME A1 A2 A3
a 1 10 100
b 2 20 200
c 3 0 0
d 0 30 0
e 0 0 400

任何建议都会有所帮助!! 非常感谢!!!

3 个答案:

答案 0 :(得分:1)

您可以使用重塑

library(reshape)
data <- merge_recurse(list(table1, table2, table3))

缺少值将为NA。将NA替换为0

data[is.na(data)] <- 0

enter image description here

答案 1 :(得分:1)

如果您想不使用包和普通的嵌套合并功能,可以将其改成:

Data :
    table1<-data.frame(NAME=c("a","b","c"), A1=c(1,2,3))
    table2<-data.frame(NAME=c("a","b","d"),A2=c(10,20,30))
    table3<-data.frame(NAME=c("a","b","e"),A3=c(100,200,400))


Code:
final_data<-merge( merge( table1, table2, by = "NAME", all = TRUE ), table3, 
       by = "NAME", all = TRUE )

输出:

 NAME A1 A2  A3
1    a  1 10 100
2    b  2 20 200
3    c  3 NA  NA
4    d NA 30  NA
5    e NA NA 400

不使用简单的语法将na替换为0

final_data[is.na(final_data)]<-"0"

输出:

  NAME A1 A2  A3
1    a  1 10 100
2    b  2 20 200
3    c  3  0   0
4    d  0 30   0
5    e  0  0 400

@Ashok KS 提到了一些代码,也可以将其编写为

data<- reshape::merge_recurse(list(table1, table2,table3))

data[is.na(data)]<-0

答案 2 :(得分:1)

使用tidyverse API

library(tidyverse)

df1 <- tribble(
    ~Name, ~A1,
    "a", 1,
    "b", 2,
    "c", 3
)

df2 <- tribble(
    ~Name, ~A2,
    "a", 10,
    "b", 20,
    "d", 30
)

df3 <- tribble(
    ~Name, ~A3,
    "a", 100,
    "b", 200,
    "e", 400
)


list(df1, df2, df3) %>% 
    reduce(full_join, by = "Name") %>% 
    replace(is.na(.), 0)