繁体   English   中英

r - Select DF 基于另一个 DF 列中的值

[英]r - Select DF based on value in column of another DF

我想绑定 2 个数据框的列。 第二个 dataframe 应该在我的环境中的几个数据帧中选择,具体取决于第一个 dataframe 中的单元格的值。

样本数据

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'的值并将与该名称匹配的dataframe与'test'dataframe绑定。 因此,目标是具有以下内容:

期望的结果

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

有没有办法自动完成?

您可以使用ls到 select 一个来自全局环境的数据帧,基于它在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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM