我从R模型对象中提取了一些随机效应系数.对于随机截距,它们如下所示:
xx <- data.frame(
`Estimate.Intercept` = c(-0.1, -0.2),
`Est.Error.Intercept` = c(0.7, 0.8),
`Q5.Intercept` = c(-1.5, -1.4),
`Q95.Intercept` = c(0.7, 0.8)
)
我正在格式化.csv
报表的数据,并try 生成"长"数据.框架/TIBLE,term_type
取自列名的第一部分,term
取自第二部分.它主要适用于tidyr
包中的pivot_longer
:
tidyr::pivot_longer(
data = xx,
cols = everything(),
names_sep = '\\.',
names_to = c('term_type', 'term'),
values_to = 'term_val'
)
结果如下所示:
# A tibble: 8 x 3
term_type term term_val
<chr> <chr> <dbl>
1 Estimate Intercept -0.140
2 Est Error 0.775
3 Q5 Intercept -1.57
4 Q95 Intercept 0.773
5 Estimate Intercept -0.140
6 Est Error 0.777
7 Q5 Intercept -1.55
8 Q95 Intercept 0.792
但它发出了这样的警告:
Warning message:
Expected 2 pieces. Additional pieces discarded in 1 rows [2].
Can I use the 100 term to specify that I want the second index of the split string, but only for the second column? i.e.我要Error
而不是Est
.我已经用ifelse
修复了它,但我想知道它是否可以在调用本身内完成.我的直觉是有一些聪明的正则表达式,或者使用stringr
的东西,但我现在很困惑...