r-根据另一个DF

时间:2020-06-01 00:02:38

标签: r dataframe

我想绑定2个数据帧的列。 在我的环境中,应根据第一个数据帧中单元格的值从多个数据帧中选择第二个数据帧。

样本数据

test <- structure(list(main_activity = structure(c(1L, 9L), .Label = c("Manufacturing", 
"Manufacturing; Retail", "Manufacturing; Services", "Manufacturing; Wholesale", 
"Manufacturing; Wholesale; Services", "Retail", "Retail; Services", 
"Retail; Wholesale", "Services", "Services; Manufacturing", "Services; Retail", 
"Services; Wholesale", "Wholesale", "Wholesale; Manufacturing", 
"Wholesale; Retail", "Wholesale; Services"), class = "factor"), 
    p_l_for_period_net_income_th_eur_2019 = c(-4849.968, -4416.404
    ), Name = c("A", "B")), class = "data.frame", row.names = c(NA, 
-2L))

Manufacturing_2015 <- as.data.frame(matrix(data = c(2000)))
Services_2015 <- as.data.frame(matrix(data = c(3000)))

我想要做的是检查“ main_activity”列的值,并将与该名称匹配的数据框与“ test”数据框绑定。因此,目标是具有以下条件:

所需结果

binded_A <- cbind(test %>% filter(Name == "A"), Manufacturing_2015)
binded_B <- cbind(test %>% filter(Name == "B"), Services_2015)

有没有办法自动做到?

1 个答案:

答案 0 :(得分:1)

您可以使用ls根据main_activity中的值从全局环境中选择一个数据框,然后cbind将数据保存到数据框的原始子集中。

result <- lapply(test$main_activity, function(x) cbind(subset(test, 
               main_activity == x), get(ls(pattern = x, envir = .GlobalEnv))))

result

#[[1]]
#  main_activity p_l_for_period_net_income_th_eur_2019 Name   V1
#1 Manufacturing                             -4849.968    A 2000

#[[2]]
#  main_activity p_l_for_period_net_income_th_eur_2019 Name   V1
#2      Services                             -4416.404    B 3000

如果您需要将它们作为单独的数据框命名,请使用list2env

names(result) <- paste0('binded_', test$Name)
list2env(result, .GlobalEnv)