Context

我正在学习this website的整齐测评, 我看到了一个例子:

x <- sym("height")

expr(transmute(starwars, bmi = mass / (!! x)^2))
#> transmute(starwars, bmi = mass/(height)^2)

transmute(starwars, bmi = mass / (!! x)^2)
#> # A tibble: 87 x 1
#>     bmi
#>   <dbl>
#> 1  26.0
#> 2  26.9
#> 3  34.7
#> 4  33.3
#> # ... with 83 more rows

然后,我使用sym!!在我自己的代码中模拟了上面的例子,但它报告了一个错误.

library(survival)
library(rms)
data(cancer)

x = sym('meal.cal')

expr(cph(Surv(time, status) ~ rcs(!! x), data = lung))
# cph(Surv(time, status) ~ rcs(meal.cal), data = lung)

cph(Surv(time, status) ~ rcs(!!x), data = lung)
# Error in !x : invalid argument type

Question

为什么我在代码中使用sym!!会报告错误以及如何修复它.

是不是因为rcs().

推荐答案

我们可以使用rlang::inject()将带有!!的参数拼接到通常不支持整齐计算的函数中.这使我们不必使用eval(expr(...)),也回答了您的问题,为什么我们不需要使用dplyr::transmute()rlang::inject().后者已经支持整齐的计算,而cph()不支持.

library(survival)
library(rms)
data(cancer)

x = sym('meal.cal')

rlang::inject(cph(Surv(time, status) ~ rcs(!!x), data = lung))
#> Frequencies of Missing Values Due to Each Variable
#> Surv(time, status)           meal.cal 
#>                  0                 47 
#> 
#> Cox Proportional Hazards Model
#>  
#>  cph(formula = Surv(time, status) ~ rcs(meal.cal), data = lung)
#>  
#>  
#>                          Model Tests    Discrimination    
#>                                                Indexes    
#>  Obs        181    LR chi2      0.72    R2       0.004    
#>  Events     134    d.f.            4    R2(4,181)0.000    
#>  Center -0.3714    Pr(> chi2) 0.9485    R2(4,134)0.000    
#>                    Score chi2   0.76    Dxy      0.048    
#>                    Pr(> chi2) 0.9443                      
#>  
#>              Coef    S.E.   Wald Z Pr(>|Z|)
#>  meal.cal    -0.0006 0.0013 -0.48  0.6299  
#>  meal.cal'    0.0007 0.0051  0.14  0.8860  
#>  meal.cal''   0.0010 0.0261  0.04  0.9682  
#>  meal.cal''' -0.0132 0.0676 -0.19  0.8456  
#> 

如果没有整齐的计算,我们可以保持R为基数,使用eval(bquote(...)),拼接x.(x).

library(survival)
library(rms)
data(cancer)

x = sym('meal.cal')

eval(bquote(cph(Surv(time, status) ~ rcs(.(x)), data = lung)))
#> Frequencies of Missing Values Due to Each Variable
#> Surv(time, status)           meal.cal 
#>                  0                 47 
#> 
#> Cox Proportional Hazards Model
#>  
#>  cph(formula = Surv(time, status) ~ rcs(meal.cal), data = lung)
#>  
#>  
#>                          Model Tests    Discrimination    
#>                                                Indexes    
#>  Obs        181    LR chi2      0.72    R2       0.004    
#>  Events     134    d.f.            4    R2(4,181)0.000    
#>  Center -0.3714    Pr(> chi2) 0.9485    R2(4,134)0.000    
#>                    Score chi2   0.76    Dxy      0.048    
#>                    Pr(> chi2) 0.9443                      
#>  
#>              Coef    S.E.   Wald Z Pr(>|Z|)
#>  meal.cal    -0.0006 0.0013 -0.48  0.6299  
#>  meal.cal'    0.0007 0.0051  0.14  0.8860  
#>  meal.cal''   0.0010 0.0261  0.04  0.9682  
#>  meal.cal''' -0.0132 0.0676 -0.19  0.8456  
#> 

reprex package(v2.0.1)于2022-09-26创建

R相关问答推荐

如何在弹性表中为类别值的背景上色

将Multilinetring合并到一个线串中,使用sf生成规则间隔的点

过滤矩阵以获得R中的唯一组合

如何求解arg必须为NULL或deSolve包的ode函数中的字符向量错误

更改编号列表的 colored颜色

pickerInput用于显示一条或多条geom_hline,这些线在图中具有不同 colored颜色

在"gt"表中添加第二个"groupname_col",而不连接列值

在数学中正确显示摄氏度、开氏度或华氏度

如何直接从Fortran到R的数组大小?

如何得到R中唯一的组合群?

在R中使用download. file().奇怪的URL?

在连续尺度上转置标签[瀑布图,R]

在df中保留原始变量和新变量

传递ggplot2的变量作为函数参数—没有映射级别以正确填充美学

使用for循环和粘贴创建多个变量

如何对r中包含特定(未知)文本的行求和?

在点图上绘制置信度或预测区间ggplot2

替换在以前工作的代码中有x行&q;错误(geom_sf/gganimate/dow_mark)

希望解析和复制R中特定模式的数据

从多行中 Select 最小值