R 如何跨多个列应用复杂的条件更改?
我有两个由不同脚本生成的数据帧。我想使用一个来修改另一个,这是基于我必须检查每个列的条件 DF1 DF2 这段代码执行我想对单个案例执行的操作,但我不知道如何使其对所有后续列都起作用。我有很多要检查的内容,列的名称和数量将根据生成上述数据帧的脚本所使用的数据而有所不同R 如何跨多个列应用复杂的条件更改?,r,dplyr,R,Dplyr,我有两个由不同脚本生成的数据帧。我想使用一个来修改另一个,这是基于我必须检查每个列的条件 DF1 DF2 这段代码执行我想对单个案例执行的操作,但我不知道如何使其对所有后续列都起作用。我有很多要检查的内容,列的名称和数量将根据生成上述数据帧的脚本所使用的数据而有所不同 testVect <- DF1 %>% filter(a_1000 == 1) testVect <- testVect %>% select(-contains("a_")) DF2
testVect <- DF1 %>% filter(a_1000 == 1)
testVect <- testVect %>% select(-contains("a_"))
DF2 <- DF2 %>% mutate(aDG_1000 = (a_1000 == 1 & dgCondition %in% testVect$dgCondition)*1)
我曾尝试构建一个for循环来遍历每一列,但没有任何运气。我也一直在研究和lapply打电话的可能性。但我也没那么幸运。我觉得我错过了一些可以让这项工作成功的东西,所以我想我应该问问专家
感谢您的帮助。这里有一种方法,它以长格式旋转所有内容
图书馆(tidyverse)
DF1A基本R方法:
result <- cbind(df2[1], mapply(function(x, y) {
as.integer(df2$dgCondition %in% x & y == 1)
}, lapply(df1[-1], function(x) df1$dgCondition[x == 1]), df2[-1]))
result
# dgCondition a_1000 a_1001 a_1010 a_1011
#1 1 0 0 0 0
#2 2 0 1 1 1
#3 1 0 0 0 0
#4 5 1 0 0 0
#5 7 0 0 0 0
#6 7 0 0 0 0
#7 7 0 0 0 0
#8 1 0 0 0 0
#9 7 0 0 0 1
#10 4 0 0 0 0
#11 4 0 0 0 0
#12 4 0 0 0 0
#13 4 0 0 0 0
#14 4 0 0 0 0
#15 6 0 0 0 0
#16 6 0 0 0 0
#17 6 0 0 1 0
#18 6 0 0 1 0
result我认为示例中有一个错误:您可能是指%testVect$dgCondition
中的dgCondition%,而不是%DF1$dgCondition
中的%ah您是对的。效果很好。我还没有用过lappy
和mappy
,看来我得好好读一读了。我不清楚的一件事是为什么在[]
中使用-1?这是忽略两个数据帧中的第一列,即dgCondition
。
testVect <- DF1 %>% filter(a_1000 == 1)
testVect <- testVect %>% select(-contains("a_"))
DF2 <- DF2 %>% mutate(aDG_1000 = (a_1000 == 1 & dgCondition %in% testVect$dgCondition)*1)
dgCondition aDG_1000
1 0
2 0
1 0
5 1
7 0
7 0
7 0
1 0
7 0
4 0
4 0
4 0
4 0
4 0
6 0
6 0
6 0
6 0
conds_for_DF1 <- pivot_longer(DF1,
starts_with("a_"),
names_to = "a_xxx") %>%
filter(value == 1) %>%
group_by(a_xxx) %>%
summarize(dgConds_DF1 = list(dgCondition),
dgConds_readable = paste(dgCondition, collapse=", "))
#> `summarise()` ungrouping output (override with `.groups` argument)
conds_for_DF1
#> # A tibble: 4 x 3
#> a_xxx dgConds_DF1 dgConds_readable
#> <chr> <list> <chr>
#> 1 a_1000 <int [3]> 3, 5, 8
#> 2 a_1001 <int [1]> 2
#> 3 a_1010 <int [4]> 2, 3, 6, 8
#> 4 a_1011 <int [3]> 2, 5, 7
pivot_longer(DF2,
starts_with("a_"),
names_to = "a_xxx") %>%
left_join(conds_for_DF1, by= "a_xxx") %>%
mutate(dgCond_in_DF1 = map2_lgl(dgCondition, dgConds_DF1, ~ .x %in% .y),
aDG_xxx = value * dgCond_in_DF1)
#> # A tibble: 72 x 7
#> dgCondition a_xxx value dgConds_DF1 dgConds_readable dgCond_in_DF1 aDG_xxx
#> <int> <chr> <int> <list> <chr> <lgl> <int>
#> 1 1 a_1000 1 <int [3]> 3, 5, 8 FALSE 0
#> 2 1 a_1001 1 <int [1]> 2 FALSE 0
#> 3 1 a_1010 0 <int [4]> 2, 3, 6, 8 FALSE 0
#> 4 1 a_1011 0 <int [3]> 2, 5, 7 FALSE 0
#> 5 2 a_1000 0 <int [3]> 3, 5, 8 FALSE 0
#> 6 2 a_1001 1 <int [1]> 2 TRUE 1
#> 7 2 a_1010 1 <int [4]> 2, 3, 6, 8 TRUE 1
#> 8 2 a_1011 1 <int [3]> 2, 5, 7 TRUE 1
#> 9 1 a_1000 0 <int [3]> 3, 5, 8 FALSE 0
#> 10 1 a_1001 0 <int [1]> 2 FALSE 0
#> # ... with 62 more rows
DF2 %>%
mutate(row_id = row_number()) %>%
pivot_longer(starts_with("a_"),
names_to = "a_xxx") %>%
left_join(conds_for_DF1, by= "a_xxx") %>%
mutate(dgCond_in_DF1 = map2_lgl(dgCondition, dgConds_DF1, ~ .x %in% .y),
aDG_xxx = value * dgCond_in_DF1) %>%
select(row_id, dgCondition, a_xxx, aDG_xxx) %>%
pivot_wider(names_from = "a_xxx",
values_from = "aDG_xxx") %>%
select(-row_id)
#> # A tibble: 18 x 5
#> dgCondition a_1000 a_1001 a_1010 a_1011
#> <int> <int> <int> <int> <int>
#> 1 1 0 0 0 0
#> 2 2 0 1 1 1
#> 3 1 0 0 0 0
#> 4 5 1 0 0 0
#> 5 7 0 0 0 0
#> 6 7 0 0 0 0
#> 7 7 0 0 0 0
#> 8 1 0 0 0 0
#> 9 7 0 0 0 1
#> 10 4 0 0 0 0
#> 11 4 0 0 0 0
#> 12 4 0 0 0 0
#> 13 4 0 0 0 0
#> 14 4 0 0 0 0
#> 15 6 0 0 0 0
#> 16 6 0 0 0 0
#> 17 6 0 0 1 0
#> 18 6 0 0 1 0
result <- cbind(df2[1], mapply(function(x, y) {
as.integer(df2$dgCondition %in% x & y == 1)
}, lapply(df1[-1], function(x) df1$dgCondition[x == 1]), df2[-1]))
result
# dgCondition a_1000 a_1001 a_1010 a_1011
#1 1 0 0 0 0
#2 2 0 1 1 1
#3 1 0 0 0 0
#4 5 1 0 0 0
#5 7 0 0 0 0
#6 7 0 0 0 0
#7 7 0 0 0 0
#8 1 0 0 0 0
#9 7 0 0 0 1
#10 4 0 0 0 0
#11 4 0 0 0 0
#12 4 0 0 0 0
#13 4 0 0 0 0
#14 4 0 0 0 0
#15 6 0 0 0 0
#16 6 0 0 0 0
#17 6 0 0 1 0
#18 6 0 0 1 0