r - 如何使用行构建列从宽到长

标签 r reshape dplyr

我需要使用其中一列来创建其他列,将数据集从宽格式重新调整为长格式。

我的数据如下所示:

Participant  V1     V2    V3   V4   V5   V6   V7   V8   V9   V10 ... V1000

Prob1_1     323.25   325.85
Prob1_2     236.12   455.23
Prob2_3     423.52   526.14     ....
Prob2_4     512.47   426.12
....
Prob2_100   235.14   632.14
Improb1_1   632.12   236.12

我想使用变量 Participant 来构建以下长数据集。

Participant    Probability    RT          Trial     Session
    1              Prob           323.25     1          1
    1              Prob           325.85     2          1
    2              Prob           236.12     1          1
    2              Prob           455.23     2          1 
    3              Prob           423.52     1          2
    3              Prob           526.14     2          2
    4        
    5
    6...

我尝试使用 mutate 函数,但它似乎取决于列名称,并且我希望它从实际参与者的名称驱动。例如“Prob1_1”,Prob之后的数字代表 session ,最后一个数字是参与者编号。变量V1、V2...代表试验次数。

How do I convert a wide dataframe to a long dataframe for a multilevel structure with 'quadruple nesting'?中提到的解决方案不适合我。

最佳答案

使用 tidyr::extract 我们可以根据以下正则表达式将参与者分成三组/列:

  • 1+ 非数字
  • 1+ 位数字
  • 0 或 1 _“不要将其分配给组/列”,
  • 1+ 位数字

然后收集变异

library(dplyr)
library(tidyr)
extract(df, Participant, into = c('Probability','Session','Participant'), 
                         regex = "^(\\D+)(\\d+)_*(\\d+)") %>% 
    gather(Trial, RT, -c('Probability','Participant','Session')) %>% 
    mutate(Trial=sub('V','',Trial)) %>% 
    select(Participant, Probability, RT, Trial, Session) %>% 
    arrange(Participant, Session, Trial)

    Participant Probability     RT Trial Session
  1           1        Prob 323.25     1       1
  2           1        Prob 325.85     2       1
  3           2        Prob 236.12     1       1
  4           2        Prob 455.23     2       1
  5           3        Prob 423.52     1       2
  6           3        Prob 526.14     2       2
  7           4        Prob 512.47     1       2
  8           4        Prob 426.12     2       2

数据

df <- structure(list(Participant = structure(1:4, .Label = c("Prob1_1", 
    "Prob1_2", "Prob2_3", "Prob2_4"), class = "factor"), V1 = c(323.25, 
    236.12, 423.52, 512.47), V2 = c(325.85, 455.23, 526.14, 426.12
    )), class = "data.frame", row.names = c(NA, -4L))

关于r - 如何使用行构建列从宽到长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57399976/

相关文章:

linux - 在 R 中运行命令后变量意外更改

r - 当我加载 ggfortify 时,自动绘图函数的行为有所不同

r - R中的快速采样

r - 通过 "multiplying"通过其他列的名称转换列的元素

r - Shiny 的 group_by dplyr 函数

r - 提取数据集中的第一个和最后一个位置

r - 用不同的扩展扩展向量

python - 有没有办法将列 reshape 为行,但行是由 ID 列组成的

r - 按个人可以接受两种治疗的治疗类型总结(计数/频率)

python - 如何将行字符串值转换为特征