数据集:https://github.com/onkaparinga/default/blob/main/pmap.csv
目的:计算每一行的OR值和95%CI
代码:
library(tidyverse)
library(epiDisplay)
df <- read_csv('pmap.csv')
#定义fun1返回or值
fun1 <- function(x){
cci <- cci(x[[1]],x[[2]],x[[3]],x[[4]])
return(cci$or)
}
#定义fun2返回or值和95%ci
fun2 <- function(x){
cci <- cci(x[[1]],x[[2]],x[[3]],x[[4]])
res <- c(cci$or,cci$ci.or)
return(res)
}
#报错
df %>%
mutate(or = pmap(.,fun1))
报错信息:
Error in mutate()
:
ℹ In argument: or = pmap(., fun1)
.
Caused by error in pmap()
:
ℹ In index: 1.
Caused by error in .f()
:
! unused arguments (X1 = .l[[1]][], X2 = .l[[2]][], X3 = .l[[3]][], X4 = .l[[4]][])
#正常运行
apply(df,1,fun1)
apply(df,1,fun1)
[1] 0.1590909 0.4705882 1.4603175 1.2481338 1.0118774 0.6350434 0.3777606 0.7544643 2.5869565 1.0614035 0.9591837
[12] 0.8758328 3.2000000 0.5620024 0.2222222 1.2153700 0.7777778 0.8064516 0.4160305 0.8720497 0.8066435 0.7455507
#不使用自定义fun函数,直接写成匿名函数也可正常运行
df %>%
mutate(or = pmap_dbl(.,~cci(..1,..2,..3,..4)$or))
单独使用fun1也可以运行
fun1(list(1,4,11,7))
fun1(c(1,4,11,7))
fun2可返回三个值,但是fun1这步都没过去,所以还没用上fun2