提问人:elka 提问时间:11/15/2023 更新时间:11/15/2023 访问量:59
使用列名和前缀在 across 语句中合并
Coalesce inside an across statement using column name and a prefix
问:
再一次,我觉得我应该能够做一些事情,看起来并不复杂,但我无法弄清楚。
我有一个带有变量等的数据帧。我想做的就是我的数据帧,以便和df
a1, v_a1, a2, v_a2
mutate
a1 = coalesce(a1, v_a1)
a2 = coalesce(a2, v_a2)
我尝试了很多不同的东西,但保持它还没有完全起作用。
这是一个玩具示例和我尝试过的东西:
df <- data.frame(
a1 = c(1,NA,2,NA),
v_a1 = c(NA,0,NA,5),
a2 = c(4,NA,8,NA),
v_a2 = c(NA,7,NA,NA),
a11 = c("just here","to force","the use of paste0","inside the coalesce"))
df %>% mutate(
across(c(a1,a2),
~ coalesce(!!!select(matches(paste0(cur_column(),"$"))))))
这会引发一个错误,即“只能在内部使用”。当我第一次声明一个带有 df 和变量参数的函数来包装合并时,我遇到了同样的错误,但它似乎不欣赏内部的cur_column()
across()
.data[[cur_column()]]
across
df %>% mutate(
across(c(a1,a2),
~ coalesce(across(all_of(c(cur_column(), paste0("v_", cur_column())))))))
这个没有错误,但输出真的很奇怪,将小块变成字符串并进行一些粘贴。这让我感到困惑。
这个主题非常接近如何根据前缀模式合并列块,但在我没有的原始字符串中使用(因此需要并且它使用for循环,而我认为我应该能够在...但也许不是!coalesce
contains
cur_column
across
任何帮助或建议,非常感谢!
答:
1赞
jkatam
11/15/2023
#1
请尝试以下代码
library(tidyverse)
df2 <- purrr::map2_dfc(df[,str_detect(names(df),'\\ba1\\b|\\ba2\\b')], df[,str_detect(names(df),'v_')], ~ coalesce(.x,.y) )
df3 <- cbind(df2,df[,str_detect(names(df),'v_|a11')])
a1 a2 v_a1 v_a2 a11
1 1 4 NA NA just here
2 0 7 0 7 to force
3 2 8 NA NA the use of paste0
4 5 NA 5 NA inside the coalesce
请检查下面的代码across
df %>% mutate(across(c(a1,a2), ~ coalesce(.x, get(paste('v',cur_column(),sep = '_'))) ))
a1 v_a1 a2 v_a2 a11
1 1 NA 4 NA just here
2 0 0 7 7 to force
3 2 NA 8 NA the use of paste0
4 5 5 NA NA inside the coalesce
评论
0赞
elka
11/15/2023
谢谢@jkatam的回答。它不能完全回答我的问题,因为它没有在 mutate 中使用 across 语句。所以我知道我可以按照你的方式去做,首先用 for 循环或 purrr:map 合并我想要的所有变量,然后是 cbind,但这不是我想要的,如果可能的话
0赞
jkatam
11/15/2023
@elka,根据您的期望,我更新了代码以使用 across。希望对您有所帮助
1赞
elka
11/15/2023
噢噢这正是我想要的:)我错过了以避免错误“由cur_column()中的错误引起!只能在 across() 中使用”get()
0赞
elka
11/15/2023
#2
好吧,不知道为什么以及如何我昨天没有找到这个,特别是当我尝试使用自定义合并函数时,但无论如何,找到了一些有效且如此简单的东西:
library(dplyr)
df <- data.frame(
a1 = c(1,NA,2,NA),
v_a1 = c(NA,0,NA,5),
a2 = c(4,NA,8,NA),
v_a2 = c(NA,7,NA,NA),
a11 = c("just","another","column","character"))
my_coalesce <- function(df,x){
vx <- paste0("v_", x)
return(coalesce(df[[x]], df[[vx]]))
}
df %>% mutate(across(c(a1, a2),
~ my_coalesce(df=.data, x=cur_column())))
虽然您首先需要该函数来避免“由错误引起:!只能在内部使用。cur_column()
across()
评论