【问题标题】:Wide to Long format with multiple variables? [duplicate]具有多个变量的宽到长格式? [复制]
【发布时间】:2018-08-30 15:53:40
【问题描述】:

我希望将数据框从宽格式转换为长格式,同时保持多列。

这里是示例数据:

df <- read.table(header=T, text='
 Subject Day Correct1 Correct2 Correct3  Percent1 Percent2 Percent3
                 1  1   1   0   1   50  25  70
                 2  1   1   0   0   75  30  80
                 3  1   0   1   1   70  45  90
                 4  1   0   1   0   80  50  100
                 5  1   1   1   1   90  60  100
                 1  2   0   1   0   30  75  90
                 2  2   0   0   1   45  70  80
                 3  2   1   1   0   50  30  90
                 4  2   1   0   0   60  45  100
                 5  2   1   1   1   80  45  90
                                  ')

并且希望它看起来像这样——我有一个正确和百分比列。

Subject Day Correct CorrectValue    Percent PercentValue
1        1  1          1               1         50
2        1  1          1               1         75
3        1  1          0               1         70
4        1  1          0               1         80
5        1  1          1               1         90
1        1  2          0               2         25
2        1  2          0               2         30
3        1  2          1               2         45
4        1  2          1               2         50
5        1  2          1               2         60
1        1  3          1               3         70
2        1  3          0               3         80
3        1  3          1               3         90
4        1  3          0               3        100
5        1  3          1               3        100
1        2  1          0               1         30
2        2  1          0               1         45
3        2  1          1               1         50
4        2  1          1               1         60
5        2  1          1               1         80
1        2  2          1               2         75
2        2  2          0               2         70
3        2  2          1               2         30
4        2  2          0               2         45
5        2  2          1               2         45
1        2  3          0               3         90
2        2  3          1               3         80
3        2  3          0               3         90
4        2  3          0               3        100
5        2  3          1               3         90

谢谢!

【问题讨论】:

标签: r


【解决方案1】:

gather 来自tidyr

library(dplyr)
library(tidyr)

df %>%
  gather(Correct, CorrectValue, Correct1:Correct3) %>%
  gather(Percent, PercentValue, Percent1:Percent3) %>%
  mutate_at(vars(Correct, Percent), ~sub("[[:alpha:]]+", "", .))

结果:

   Subject Day Correct CorrectValue Percent PercentValue
1        1   1       1            1       1           50
2        2   1       1            1       1           75
3        3   1       1            0       1           70
4        4   1       1            0       1           80
5        5   1       1            1       1           90
6        1   2       1            0       1           30
7        2   2       1            0       1           45
8        3   2       1            1       1           50
9        4   2       1            1       1           60
10       5   2       1            1       1           80
11       1   1       2            0       1           50
12       2   1       2            0       1           75
13       3   1       2            1       1           70
14       4   1       2            1       1           80
15       5   1       2            1       1           90
16       1   2       2            1       1           30
17       2   2       2            0       1           45
18       3   2       2            1       1           50
19       4   2       2            0       1           60
20       5   2       2            1       1           80
21       1   1       3            1       1           50
22       2   1       3            0       1           75
23       3   1       3            1       1           70
24       4   1       3            0       1           80
25       5   1       3            1       1           90
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 2023-03-19
    相关资源
    最近更新 更多