从长到宽,具有基于转换变量计数的唯一 ID

Long to wide with a unique ID based on the count of the transformed variable

我有这个宽格式的数据,s02的宽度最多占三个,即0,1,2

id_1<-c(1,2,2,2)
s02_0<-c(1,1,4,7)
s02_1<-c(2,2,5,8)
s02_2<-c(3,3,6,9)
id_2<-c(1,1,2,3)

我希望重塑我的数据,并创建一个额外的列,比如 n,它显示每个给定行中 's02_' 占据的位置。 我的预期输出如下

id_1<-c(1,1,1,2,2,2,2,2,2,2,2,2)
s02<-c(1,2,3,1,2,3,4,5,6,7,8,9)
n<-c(1,2,3,1,2,3,1,2,3,1,2,3)
df2<-data.frame(id_1,s02,n)

这是一个几乎只有一行的 tidyverse 版本:

library(tidyr)
pivot_longer(select(df1, -id_2), starts_with("s02_"),
             names_to = "n", names_prefix = "s02_", values_to = "s02")
# # A tibble: 12 x 3
#     id_1 n       s02
#    <dbl> <chr> <dbl>
#  1     1 0         1
#  2     1 1         2
#  3     1 2         3
#  4     2 0         1
#  5     2 1         2
#  6     2 2         3
#  7     2 0         4
#  8     2 1         5
#  9     2 2         6
# 10     2 0         7
# 11     2 1         8
# 12     2 2         9

注意:这个n是从0开始的,因为它的值是从列名s02_0s02_1s02_2派生的;这可以通过一些 post 处理来解决(例如,转换为整数,加一,可选择转换回字符),例如

library(dplyr)
pivot_longer(select(df1, -id_2), starts_with("s02_"),
             names_to = "n", names_prefix = "s02_", values_to = "s02") %>%
  mutate(n = as.integer(n) + 1L)
# # A tibble: 12 x 3
#     id_1     n   s02
#    <dbl> <int> <dbl>
#  1     1     1     1
#  2     1     2     2
#  3     1     3     3
#  4     2     1     1
#  5     2     2     2
#  6     2     3     3
#  7     2     1     4
#  8     2     2     5
#  9     2     3     6
# 10     2     1     7
# 11     2     2     8
# 12     2     3     9