我想知道是否/如何将下面的调用转换为可以在我经常使用我的数据执行的任务中使用的函数。遗憾的是,我无法弄清楚如何从涉及mutate
和case_when
的调用设计函数,这两个函数都依赖于dplyr
包并需要多个额外的参数
或者,对于我这么多case_when
来说,呼叫本身似乎是多余的,也许可以减少其使用次数。
欢迎任何有关替代方法的帮助和信息。
电话看起来像这样:
library(dplyr)
library(stringr)
test_data %>%
mutate(
multipleoptions_o1 = case_when(
str_detect(multipleoptions, "option1") ~ 1,
is.na(multipleoptions) ~ NA_real_,
TRUE ~ 0),
multipleoptions_o2 = case_when(
str_detect(multipleoptions, "option2") ~ 1,
is.na(multipleoptions) ~ NA_real_,
TRUE ~ 0),
multipleoptions_o3 = case_when(
str_detect(multipleoptions, "option3") ~ 1,
is.na(multipleoptions) ~ NA_real_,
TRUE ~ 0),
multipleoptions_o4 = case_when(
str_detect(multipleoptions, "option4") ~ 1,
is.na(multipleoptions) ~ NA_real_,
TRUE ~ 0)
)
示例数据:
structure(list(multipleoptions = c("option1", "option2", "option3",
NA, "option2,option3", "option4")), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
函数的期望输出:
structure(list(multipleoptions = c("option1", "option2", "option3",
NA, "option2,option3", "option4"), multipleoptions_o1 = c(1,
0, 0, NA, 0, 0), multipleoptions_o2 = c(0, 1, 0, NA, 1, 0), multipleoptions_o3 = c(0,
0, 1, NA, 1, 0), multipleoptions_o4 = c(0, 0, 0, NA, 0, 1)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -6L))
函数的参数应该是:data
(即输入数据集),multipleoptions
(即包含答案选项的数据中的列,总是一列),patterns_to_look_for
(即,在多个选项中查找的str_detect模式,number_of_options
,理想情况下,选项的数量可以多于或少于4,(我不确定它是否可以实现),output_columns
(即新列的名称,它始终是名称或原始列,后跟选项号或选项名称)。
答案 0 :(得分:4)
您可以通过将选项拆分为单独的元素来避免冗长的case_when
代码,利用嵌套/取消来获取单列选项,然后进行扩展以获得每个选项的单独列。
更新了答案
library(tidyverse)
# Arguments
# data A data frame
# patterns Regular expression giving the pattern(s) at which to split the options strings
# ... Grouping columns, the first of which must be the "options" column.
# If options has repeated values, then there must be a second grouping
# column (an "ID" column) to differentiate these repeated values.
fnc = function(data, patterns, ...) {
col = quos(...)
data %>%
mutate(option=str_split(!!!col[[1]], patterns)) %>%
unnest %>%
mutate(value=1) %>%
group_by(!!!col) %>%
mutate(num_chosen = ifelse(is.na(!!!col[[1]]), 0, sum(value))) %>%
spread(option, value, fill=0) %>%
select_at(vars(-matches("NA")))
}
fnc(test_data, ",", multipleoptions)
multipleoptions num_chosen option1 option2 option3 option4 1 option1 1 1 0 0 0 2 option2 1 0 1 0 0 3 option2,option3 2 0 1 1 0 4 option3 1 0 0 1 0 5 option4 1 0 0 0 1 6 <NA> 0 0 0 0 0
# Fake data
ops = paste0("option",1:4)
set.seed(2)
d = data_frame(var=replicate(20, paste(sample(ops, sample(1:4,1, prob=c(10,8,5,1))), collapse=",")))
# Add missing values
d = bind_rows(d[1:5,], data.frame(var=rep(NA,3)), d[6:nrow(d),])
fnc(d %>% mutate(ID=1:n()), ",", var, ID)
var ID num_chosen option1 option2 option3 option4 1 option1 17 1 1 0 0 0 2 option1,option2 12 2 1 1 0 0 3 option1,option2,option3 5 3 1 1 1 0 4 option1,option2,option4,option3 9 4 1 1 1 1 5 option1,option3 2 2 1 0 1 0 6 option1,option3,option4 3 3 1 0 1 1 7 option1,option4,option2 20 3 1 1 0 1 8 option1,option4,option3,option2 13 4 1 1 1 1 9 option2 11 1 0 1 0 0 10 option2,option3 23 2 0 1 1 0 11 option2,option3,option4 21 3 0 1 1 1 12 option3 1 1 0 0 1 0 13 option3 15 1 0 0 1 0 14 option3,option1 4 2 1 0 1 0 15 option3,option2,option4 14 3 0 1 1 1 16 option3,option4,option2,option1 22 4 1 1 1 1 17 option4 10 1 0 0 0 1 18 option4 16 1 0 0 0 1 19 option4 18 1 0 0 0 1 20 option4,option2,option3 19 3 0 1 1 1 21 <NA> 6 0 0 0 0 0 22 <NA> 7 0 0 0 0 0 23 <NA> 8 0 0 0 0 0
原始答案
test_data %>%
filter(!is.na(multipleoptions)) %>%
mutate(option=str_split(multipleoptions, ",")) %>%
unnest %>%
mutate(value=1) %>%
spread(option, value)
multipleoptions option1 option2 option3 option4 <chr> <dbl> <dbl> <dbl> <dbl> 1 option1 1 NA NA NA 2 option2 NA 1 NA NA 3 option2,option3 NA 1 1 NA 4 option3 NA NA 1 NA 5 option4 NA NA NA 1
将其打包成一个函数:
fnc = function(data, col, patterns) {
col = enquo(col)
data %>%
filter(!is.na(!!col)) %>%
mutate(option=str_split(!!col, patterns)) %>%
unnest %>%
mutate(value=1) %>%
spread(option, value)
}
fnc(test_data, multipleoptions, ",")
如果您的真实数据具有多个具有相同值multipleoptons
的行,则此代码仅在还有ID
列的情况下才有效,该列区分具有相同值{{}的不同行1}}。例如:
multipleoptions
错误:行(1,27),(16,28,30)的重复标识符
# Fake data
ops = paste0("option",1:4)
set.seed(2)
d = data.frame(var=replicate(20, paste(sample(ops, sample(1:4,1, prob=c(10,8,5,1))), collapse=",")))
fnc(d, var, ",")
# Add unique row identifier fnc(d %>% mutate(ID = 1:n()), var, ",")