如何应用跨多列具有多个输入的函数?
How do you apply a function that has multiple inputs across multiple columns?
我有一个接受两个参数(列)的函数。该函数根据另一列中的值 (y) 更改一列 (x) 的值。
fun <- function(x, y) {
x = coalesce(case_when(y %in% c("hello", "hi") ~ '1',
y == "thanks" ~ '2'), x)
}
但是,这需要在很多列对上完成,所以我想把它变成一个函数。
这样做正确吗:
df %>% mutate(across(c(col1, col3), c(col2, col4), fun))
来自
col1 col2 col3 col4
1 1
2 4
5 "hello" 5 "hello"
3
4 4
5 "hi" 5 "thanks"
5 "thanks"
5 "goodbye" 5 "hello"
到
col1 col2 col3 col4
1 1
2 4
1 "hello" 1 "hello"
3
4 4
1 "hi" 2 "thanks"
2 "thanks"
5 "goodbye" 1 "hello"
如果是成对的,那么我们可能需要map2
其中returns个list
个vector
个可以赋值给新列到现有列的数据集(代码不清楚)
library(purrr)
library(dplyr)
fun <- function(data, x, y) {
coalesce(case_when(data[[y]] %in% c("hello", "hi") ~ 1,
data[[y]] == "thanks" ~ 2), data[[x]])
}
df[c("col1", "col3")] <- map2( c("col1", "col3"),
c("col2", "col4"), ~ fun(df, .x, .y))
-输出
> df
col1 col2 col3 col4
1 1 <NA> 1 <NA>
2 2 <NA> 4 <NA>
3 1 hello 1 hello
4 3 <NA> NA <NA>
5 4 <NA> 4 <NA>
6 1 hi 2 thanks
7 2 thanks NA <NA>
8 5 goodbye 1 hello
数据
df <- structure(list(col1 = c(1L, 2L, 5L, 3L, 4L, 5L, 5L, 5L), col2 = c(NA,
NA, "hello", NA, NA, "hi", "thanks", "goodbye"), col3 = c(1L,
4L, 5L, NA, 4L, 5L, NA, 5L), col4 = c(NA, NA, "hello", NA, NA,
"thanks", NA, "hello")), class = "data.frame", row.names = c(NA,
-8L))
我有一个接受两个参数(列)的函数。该函数根据另一列中的值 (y) 更改一列 (x) 的值。
fun <- function(x, y) {
x = coalesce(case_when(y %in% c("hello", "hi") ~ '1',
y == "thanks" ~ '2'), x)
}
但是,这需要在很多列对上完成,所以我想把它变成一个函数。
这样做正确吗:
df %>% mutate(across(c(col1, col3), c(col2, col4), fun))
来自
col1 col2 col3 col4
1 1
2 4
5 "hello" 5 "hello"
3
4 4
5 "hi" 5 "thanks"
5 "thanks"
5 "goodbye" 5 "hello"
到
col1 col2 col3 col4
1 1
2 4
1 "hello" 1 "hello"
3
4 4
1 "hi" 2 "thanks"
2 "thanks"
5 "goodbye" 1 "hello"
如果是成对的,那么我们可能需要map2
其中returns个list
个vector
个可以赋值给新列到现有列的数据集(代码不清楚)
library(purrr)
library(dplyr)
fun <- function(data, x, y) {
coalesce(case_when(data[[y]] %in% c("hello", "hi") ~ 1,
data[[y]] == "thanks" ~ 2), data[[x]])
}
df[c("col1", "col3")] <- map2( c("col1", "col3"),
c("col2", "col4"), ~ fun(df, .x, .y))
-输出
> df
col1 col2 col3 col4
1 1 <NA> 1 <NA>
2 2 <NA> 4 <NA>
3 1 hello 1 hello
4 3 <NA> NA <NA>
5 4 <NA> 4 <NA>
6 1 hi 2 thanks
7 2 thanks NA <NA>
8 5 goodbye 1 hello
数据
df <- structure(list(col1 = c(1L, 2L, 5L, 3L, 4L, 5L, 5L, 5L), col2 = c(NA,
NA, "hello", NA, NA, "hi", "thanks", "goodbye"), col3 = c(1L,
4L, 5L, NA, 4L, 5L, NA, 5L), col4 = c(NA, NA, "hello", NA, NA,
"thanks", NA, "hello")), class = "data.frame", row.names = c(NA,
-8L))