R函数,用于将不同列的多个范围从宽格式折叠为长格式?

R函数,用于将不同列的多个范围从宽格式折叠为长格式?,r,tidyverse,reshape,R,Tidyverse,Reshape,我有一个数据集,每行有多个不同范围的列(每行对应一个单独的列),如下所示。不同列类型的每个实例都有3个级别(0、1和2) 我需要的是为每个id将所有col1折叠成一列,将所有col2折叠成另一列,将所有col3折叠成另一列。如下所示 id x col1 col2 col4 1 0 0 2 3 1 1 1 2 4 1 2 3 3 5 2 0 1 2 4 2 1 1 4

我有一个数据集,每行有多个不同范围的列(每行对应一个单独的列),如下所示。不同列类型的每个实例都有3个级别(0、1和2)

我需要的是为每个id将所有col1折叠成一列,将所有col2折叠成另一列,将所有col3折叠成另一列。如下所示

id  x  col1 col2 col4
1   0     0    2    3       
1   1     1    2    4
1   2     3    3    5
2   0     1    2    4
2   1     1    4    5
2   2     1    7    5
.
.
etc.
此外,我还需要为每个id创建一个值为0、1和2的x列。但是,我仅使用下面的代码折叠第一个列范围(col1)

library(tidyverse)

longer_data <- dataframe %>%
  group_by(id) %>%
  pivot_longer(col1_0:col1_2, names_to = "x1", values_to = "col1")
库(tidyverse)
更长的数据%
分组依据(id)%>%
枢轴长度(col1\u 0:col1\u 2,名称到=“x1”,值到=“col1”)
x1在此创建一个具有原始列名的列。因此,我需要创建一个额外的x列,它只保留原始列名的最后一个数字

res <- reshape(
  df,
  direction = "long",
  idvar = "id",
  varying = -1,
  timevar = "x",
  sep = "_"
)
res <- res[order(res$id), ]

有没有办法做到这一点?非常感谢

我们不需要任何
groupby
。通过在
names\u to
中指定
names\u sep
.value
可以直接使用
pivot\u longer
完成。注意
.value
x
的顺序。它意味着该列的值应该在
\uuuu
之前进入这些前缀中的每一个,并且带有后缀存根的新列进入“x”

library(dplyr)
library(tidyr)
df1 %>%
   pivot_longer(cols = -id, names_to = c('.value', 'x'), names_sep = "_")
-输出

# A tibble: 6 x 5
#     id x      col1  col2  col3
#  <int> <chr> <int> <int> <int>
#1     1 0         0     2     3
#2     1 1         1     2     4
#3     1 2         3     3     5
#4     2 0         1     2     4
#5     2 1         1     4     5
#6     2 2         2     7     5
#一个tible:6 x 5
#id x col1 col2 col3
#      
#1     1 0         0     2     3
#2     1 1         1     2     4
#3     1 2         3     3     5
#4     2 0         1     2     4
#5     2 1         1     4     5
#6     2 2         2     7     5
数据
df1我们不需要任何
groupby
。通过在
names\u to
中指定
names\u sep
.value
可以直接使用
pivot\u longer
完成。注意
.value
x
的顺序。它意味着该列的值应该在
\uuuu
之前进入这些前缀中的每一个,并且带有后缀存根的新列进入“x”

library(dplyr)
library(tidyr)
df1 %>%
   pivot_longer(cols = -id, names_to = c('.value', 'x'), names_sep = "_")
-输出

# A tibble: 6 x 5
#     id x      col1  col2  col3
#  <int> <chr> <int> <int> <int>
#1     1 0         0     2     3
#2     1 1         1     2     4
#3     1 2         3     3     5
#4     2 0         1     2     4
#5     2 1         1     4     5
#6     2 2         2     7     5
#一个tible:6 x 5
#id x col1 col2 col3
#      
#1     1 0         0     2     3
#2     1 1         1     2     4
#3     1 2         3     3     5
#4     2 0         1     2     4
#5     2 1         1     4     5
#6     2 2         2     7     5
数据
df1这里有一个使用
restrape
的基本R选项,其中
timevar=“x”
创建一个名为
x
的列,并且
sep=“\u”
有助于获取原始列名的最后数字

res <- reshape(
  df,
  direction = "long",
  idvar = "id",
  varying = -1,
  timevar = "x",
  sep = "_"
)
res <- res[order(res$id), ]
数据

> dput(df)
structure(list(id = 1:2, col1_0 = 0:1, col1_1 = c(1L, 1L), col1_2 = 3:2,
    col2_0 = c(2L, 2L), col2_1 = c(2L, 4L), col2_2 = c(3L, 7L
    ), col3_0 = 3:4, col3_1 = 4:5, col3_2 = c(5L, 5L)), class = "data.frame", row.names = c(NA, 
-2L))

这里是一个使用
重塑
的基本R选项,其中
timevar=“x”
创建一个名为
x
的列,而
sep=“\uq”
有助于获取原始列名的最后数字

res <- reshape(
  df,
  direction = "long",
  idvar = "id",
  varying = -1,
  timevar = "x",
  sep = "_"
)
res <- res[order(res$id), ]
数据

> dput(df)
structure(list(id = 1:2, col1_0 = 0:1, col1_1 = c(1L, 1L), col1_2 = 3:2,
    col2_0 = c(2L, 2L), col2_1 = c(2L, 4L), col2_2 = c(3L, 7L
    ), col3_0 = 3:4, col3_1 = 4:5, col3_2 = c(5L, 5L)), class = "data.frame", row.names = c(NA, 
-2L))