gpt4 book ai didi

r - 使用其他列中的值跨多个列进行条件变异 - 在 tidyverse 中寻找有效的解决方案

转载 作者:行者123 更新时间:2023-12-03 08:02:13 25 4
gpt4 key购买 nike

如果满足特定条件,我喜欢将一列的值替换为另一列的值。下面是一个玩具示例,我首先手动实现此目标,然后针对该问题起草一个有点笨拙的编程解决方案。 (当然,我的真实数据包含更多变量,也需要更复杂的条件替换值)

library(dplyr)
library(purrr)

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

## example data
set.seed(123)
df <- tibble(
x = sample(0:1, 10, replace = T),
x_99 = sample(3:4, 10, replace = T),
y = sample(0:1, 10, replace = T),
y_99 = sample(3:4, 10, replace = T)
)

df
#> # A tibble: 10 × 4
#> x x_99 y y_99
#> <int> <int> <int> <int>
#> 1 0 4 0 3
#> 2 0 4 1 4
#> 3 0 4 0 3
#> 4 1 3 0 4
#> 5 0 4 0 4
#> 6 1 3 0 3
#> 7 1 4 1 3
#> 8 1 3 1 3
#> 9 0 3 0 3
#> 10 0 3 1 4

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# manual mutate

df |>
transmute(
x = ifelse(x == 0, x_99, x),
y = ifelse(y == 0, y_99, y)
)
#> # A tibble: 10 × 2
#> x y
#> <int> <int>
#> 1 4 3
#> 2 4 1
#> 3 4 3
#> 4 1 4
#> 5 4 4
#> 6 1 3
#> 7 1 1
#> 8 1 1
#> 9 3 3
#> 10 3 1

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# verbose programmatic solution

helper <- function(df, x, xnew) {
df[df[x] == 0, ][, x] <- df[df[x] == 0, ][, xnew]
return(tibble(df[x]))
}

col.vec1 <- c("x", "y")
col.vec2 <- paste0(col.vec1, "_99")

map2(
col.vec1, col.vec2,
~ helper(df, .x, .y)
) |>
bind_cols()
#> # A tibble: 10 × 2
#> x y
#> <int> <int>
#> 1 4 3
#> 2 4 1
#> 3 4 3
#> 4 1 4
#> 5 4 4
#> 6 1 3
#> 7 1 1
#> 8 1 1
#> 9 3 3
#> 10 3 1

reprex package 于 2022 年 9 月 11 日创建(v2.0.1)

我实际上主要使用 tidyverse 工具进行数据清理。但在这种情况下,这绝对超出了我的能力范围。我仍然找到了一个解决方案,但我很确定有一个更优雅且更简洁的解决方案。我非常感谢您的建议。

最佳答案

我认为across看看cur_column()会对你有用。您唯一真正需要更改的是将 c(x, y) 更改为某种清晰整洁的方式来标识列。

df %>%
mutate(
across(c(x, y), ~ if_else(. == 0L, get(paste0(cur_column(), "_99")), .))
)
# # A tibble: 10 x 4
# x x_99 y y_99
# <int> <int> <int> <int>
# 1 4 4 3 3
# 2 4 4 1 4
# 3 4 4 3 3
# 4 1 3 4 4
# 5 4 4 4 4
# 6 1 3 3 3
# 7 1 4 1 3
# 8 1 3 1 3
# 9 3 3 3 3
# 10 3 3 1 4

关于r - 使用其他列中的值跨多个列进行条件变异 - 在 tidyverse 中寻找有效的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73681041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com