我从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的东西,但我现在很困惑...

推荐答案

一些列名中有多个.(Est.Error.Intercept).最好使用names_pattern来捕获不包含任何.个字符([^.]+)的组((...)).此外,使用$指定字符串的结尾

tidyr::pivot_longer(
    data = xx, 
    cols = everything(), 
     names_pattern = "([^.]+)\\.([^.]+)$", 
    names_to = c('term_type', 'term'), 
    values_to = 'term_val'
)

-输出

# A tibble: 8 × 3
  term_type term      term_val
  <chr>     <chr>        <dbl>
1 Estimate  Intercept     -0.1
2 Error     Intercept      0.7
3 Q5        Intercept     -1.5
4 Q95       Intercept      0.7
5 Estimate  Intercept     -0.2
6 Error     Intercept      0.8
7 Q5        Intercept     -1.4
8 Q95       Intercept      0.8

"([^.]+)\\.([^.]+)$"-捕获为两组1)([^.]+)-一个或多个非.字符,后跟.(\\.)和2)第二组非.字符,直到字符串结束($).

R相关问答推荐

如何在xyplot中 for each 面板打印R^2

如何在kableextra调用cell_spec()中忽略NA?

R-更新面内部的栅格值

为什么当用osmdata映射R时会得到相邻状态?

在R中,如何将变量(A,B和C)拟合在同一列中,如A和B,以及A和C在同一面板中?

根据日期从参考帧中创建不同的帧

Select 季度月值

Ggplot2中geom_tile的动态zoom

如何将网站图像添加到带有极坐标的面包裹条形图?

在数据帧列表上绘制GGPUP

为什么我对圆周率图的蒙特卡罗估计是空的?

在具有多个响应变量的比例堆叠条形图上方添加总计

在不对R中的变量分组的情况下取两行的平均值

访问数据帧中未定义的列时出现R错误

如何判断代码是否在R Markdown(RMD)上下文中交互运行?

使用一个标签共享多个组图图例符号

如何在R中的两列以上使用联合(&U)?

将y轴格式更改为R中的百分比

创建两个变量组合的索引矩阵

如何根据分位数及其值创建格式化字符串值的数组?