如何根据字符串绑定列
How to bind columns based on character string
我正在尝试根据列的字符串将我的数据框从宽格式转换为长格式。在下面的示例中,我希望 O2_222.coefficients.x1
和 O2_217.coefficients.x1
在单列中,O2_222.R-squared
和 O2_217.R-squared
在单列中,RunTime
作为它自己的。尝试在 reshape2
包中使用 melt
,但语法似乎不正确。
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")
我们可以使用 tidyr
中的 pivot_longer
并将 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
我正在尝试根据列的字符串将我的数据框从宽格式转换为长格式。在下面的示例中,我希望 O2_222.coefficients.x1
和 O2_217.coefficients.x1
在单列中,O2_222.R-squared
和 O2_217.R-squared
在单列中,RunTime
作为它自己的。尝试在 reshape2
包中使用 melt
,但语法似乎不正确。
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")
我们可以使用 tidyr
中的 pivot_longer
并将 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