如何根据字符串绑定列

问题描述 投票:0回答:1

我试图根据一列的字符串将我的数据框架从宽格式转换为长格式。在下面的例子中,我想要 O2_222.coefficients.x1O2_217.coefficients.x1 在单列中。O2_222.R-squaredO2_217.R-squared 单列 RunTime 作为自己的。尝试使用 meltreshape2 包,但似乎无法正确地使用语法。

structure(list(O2_222.coefficients.x1 = c(0.494206524044508, 0.351865091962266,
0.348933739038027, 0.412232161883577, 0.702783684327072), `O2_222.R-squared` = 
c(0.922054236839182, 0.915753625911676, 0.91109476704698, 0.917998834313392, 
0.967759465780247), O2_217.coefficients.x1 = c(0.390012278483346, 0.0694948285748309, 
0.0323121611694059, 0.0372526286990146, 0.194291648564898), `O2_217.R-squared` = 
c(0.921256057864199, 0.537913087580067, 0.271398305115866, 0.274339042666519, 
0.908338928665188), RunTime = c(9.03, 14.08, 19.08, 24.08, 29.08)), row.names = c(116L, 
216L, 316L, 416L, 516L), class = "data.frame")
r reshape2 melt
1个回答
0
投票

我们可以使用 pivot_longertidyr 并指定 names_sep 作为 .

library(tidyr)
library(dplyr)
df1 %>%
   pivot_longer(cols = -RunTime, names_to = c('group', '.value'), names_sep="\\.")
# A tibble: 10 x 4
#   RunTime group  coefficients `R-squared`
#     <dbl> <chr>         <dbl>       <dbl>
# 1    9.03 O2_222       0.494        0.922
# 2    9.03 O2_217       0.390        0.921
# 3   14.1  O2_222       0.352        0.916
# 4   14.1  O2_217       0.0695       0.538
# 5   19.1  O2_222       0.349        0.911
# 6   19.1  O2_217       0.0323       0.271
# 7   24.1  O2_222       0.412        0.918
# 8   24.1  O2_217       0.0373       0.274
# 9   29.1  O2_222       0.703        0.968
#10   29.1  O2_217       0.194        0.908
© www.soinside.com 2019 - 2024. All rights reserved.