请考虑以下内容:
自定义函数CustomFun
带有几个数字参数。参数名称存储在resp
中,并与函数参数名称相对应。参数值存储在列val
中。
data.frame
拥有有关多个患者(id
)的信息,因此数据需要按id
分组。
问题:
我们如何将自定义函数应用于分组的data.frame
或data.table
,它们从相同数据结构中的列获取参数?
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(data.table)
#>
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#>
#> between, first, last
# The data
df.x <- data.frame(id = rep(c(1:2), each = 5),
resp = c("val.a", "val.b", "val.c", "val.d", "val.e"),
val = c(10, 15, NA, NA, NA,
1, 5, NA, NA, NA))
df.x
#> id resp val
#> 1 1 val.a 10
#> 2 1 val.b 15
#> 3 1 val.c NA
#> 4 1 val.d NA
#> 5 1 val.e NA
#> 6 2 val.a 1
#> 7 2 val.b 5
#> 8 2 val.c NA
#> 9 2 val.d NA
#> 10 2 val.e NA
# A simple function (minimal replicable example)
CustomFun <- function(a,b){
a+b
}
所需的输出:
# Desired output
df.x %>% mutate(res = c(25, 25, NA, NA, NA, 6, 6, NA, NA, NA))
#> id resp val res
#> 1 1 val.a 10 25
#> 2 1 val.b 15 25
#> 3 1 val.c NA NA
#> 4 1 val.d NA NA
#> 5 1 val.e NA NA
#> 6 2 val.a 1 6
#> 7 2 val.b 5 6
#> 8 2 val.c NA NA
#> 9 2 val.d NA NA
#> 10 2 val.e NA NA
自己的方法:
在没有组(id
)的情况下,此方法有效。对于所有非NA
或val
,在val.a
中没有val.b
不会造成问题,因为可以在第二步中将它们过滤掉。
# Approach without the need of grouping: one id only, problem: NA also assigned to val in df.z[3:5, ]
# dplyr
df.z <- df.x %>% slice(1:5)
df.z
#> id resp val
#> 1 1 val.a 10
#> 2 1 val.b 15
#> 3 1 val.c NA
#> 4 1 val.d NA
#> 5 1 val.e NA
df.z %>% mutate(test = CustomFun(a = df.z %>% filter(resp == "val.a") %>% pull(val),
b = df.z %>% filter(resp == "val.b") %>% pull(val))
)
#> id resp val test
#> 1 1 val.a 10 25
#> 2 1 val.b 15 25
#> 3 1 val.c NA 25
#> 4 1 val.d NA 25
#> 5 1 val.e NA 25
# data.table
setDT(df.z)[, .(test= CustomFun(a = setDT(df.z)[resp == "val.a", val],
b = setDT(df.z)[resp == "val.b", val])),
by = .(id, val, resp)]
#> id val resp test
#> 1: 1 10 val.a 25
#> 2: 1 15 val.b 25
#> 3: 1 NA val.c 25
#> 4: 1 NA val.d 25
#> 5: 1 NA val.e 25
# NOT working for groups =====================================
# data.frame
df.x %>%
group_by(id) %>%
mutate(test = CustomFun(a = df.x %>% filter(resp == "val.a") %>% pull(val),
b = df.x %>% filter(resp == "val.b") %>% pull(val))
)
#> Error in mutate_impl(.data, dots): Column `test` must be length 5 (the group size) or one, not 2
# data.table
setDT(df.x)[, .(test= CustomFun(a = setDT(df.x)[resp == "val.a", val],
b = setDT(df.x)[resp == "val.b", val])),
by = .(id, val, resp)]
#> id val resp test
#> 1: 1 10 val.a 25
#> 2: 1 10 val.a 6
#> 3: 1 15 val.b 25
#> 4: 1 15 val.b 6
#> 5: 1 NA val.c 25
#> 6: 1 NA val.c 6
#> 7: 1 NA val.d 25
#> 8: 1 NA val.d 6
#> 9: 1 NA val.e 25
#> 10: 1 NA val.e 6
#> 11: 2 1 val.a 25
#> 12: 2 1 val.a 6
#> 13: 2 5 val.b 25
#> 14: 2 5 val.b 6
#> 15: 2 NA val.c 25
#> 16: 2 NA val.c 6
#> 17: 2 NA val.d 25
#> 18: 2 NA val.d 6
#> 19: 2 NA val.e 25
#> 20: 2 NA val.e 6
由reprex package(v0.2.1)于2018-11-13创建
非常感谢!
答案 0 :(得分:2)
有2个不同的问题:您在data.table
中添加了不需要的分组变量,并且两个版本中的数据子集都不正确。
data.table
的调整:
setDT(df.x)[!is.na(val), test := CustomFun(a = val[resp == "val.a"],
b = val[resp == "val.b"]), by = id]
无需按resp
和val
进行分组,只需按id
进行分组。
对于dplyr
,您可以这样做:
df.x %>%
group_by(id) %>%
mutate(test = if_else(!is.na(val), CustomFun(a = val[resp == "val.a"],
b = val[resp == "val.b"]), NA_real_)
)
两种情况下的输出:
id resp val test
1: 1 val.a 10 25
2: 1 val.b 15 25
3: 1 val.c NA NA
4: 1 val.d NA NA
5: 1 val.e NA NA
6: 2 val.a 1 6
7: 2 val.b 5 6
8: 2 val.c NA NA
9: 2 val.d NA NA
10: 2 val.e NA NA
答案 1 :(得分:0)
我们可以按组对值进行子集设置(假设每个“ id”只有一个“ val.a”,“ val.b”并添加
library(dplyr)
df.x %>%
group_by(id) %>%
mutate(res = (val[resp == 'val.a'] + val[resp == 'val.b']) * NA^(is.na(val)))
# A tibble: 10 x 4
# Groups: id [2]
# id resp val res
# <int> <fct> <dbl> <dbl>
# 1 1 val.a 10 25
# 2 1 val.b 15 25
# 3 1 val.c NA NA
# 4 1 val.d NA NA
# 5 1 val.e NA NA
# 6 2 val.a 1 6
# 7 2 val.b 5 6
# 8 2 val.c NA NA
# 9 2 val.d NA NA
#10 2 val.e NA NA
或者另一种选择是filter
,按组进行summarize
,然后与原始数据集合并
df.x %>%
filter(resp %in% c('val.a', 'val.b')) %>%
group_by(id) %>%
summarise(res = sum(val)) %>%
right_join(df.x) %>%
mutate(res = replace(res, is.na(val), NA))