帮助重塑R中的大量纵向数据

时间:2020-01-08 06:27:38

标签: r reshape longitudinal

很抱歉,如果我发帖不正确。我是R的新手,这是我对stackoverflow的第一篇文章。我已经阅读了尽可能多的文章,为我的问题找到了解决方案,但是还没有找到我可以使用的东西。

我有一些密集的纵向数据正在尝试重塑。目前,它的格式很宽,看起来像这样:

Participant   D1_1_1   D1_1_2   D1_1_3   D1_1_4    D2_1_1   D2_1_2  etc...
P1               6        2        3        5        1         2
P2               4        9        3        6        4         1
P3               7        4        2        8        1         1
P4               1        5        1        1        6         7 
P5               2        0        8        2        1         4
etc..

列变量指的是对特定调查项目的响应,该响应是在特定日期,一天中的特定时间进行的。

所以:

D1_1_1 =第1天,时间1,项目1

D1_1_2 =第1天,时间1,项目2

...

D4_3_7 =第4天,时间3,项目7

我总共拥有的数据包括: 60名参与者在10天内每天对11个项目进行了4次响应(每个参与者共440个数据点)。

我正在寻求有关如何将其有效地转换为长格式的帮助,因此它看起来可能像这样:

Participant     Day     time    item 1   item 2 ... item 11
P1               1        1        6        2
P1               1        2        X        X
P1               1        3        X        X
P1               1        4        X        X
P1               2        1        1        4
etc..

其中X是参与者在特定日期,特定时间对给定调查项目的答复。

任何帮助将不胜感激!

欢呼

2 个答案:

答案 0 :(得分:1)

Ronak的答案非常有效,但是不需要使用extractpivot_longer已经可以将列分为几部分:

library(tidyr)

df %>%
  pivot_longer(cols = -Participant, names_to = c("day", "time", "item"), 
               names_pattern = "(D\\d)_(\\d)_(\\d)") %>%
  pivot_wider(names_from = item, values_from = value, names_prefix = "Item")
#> # A tibble: 10 x 7
#>    Participant day   time  Item1 Item2 Item3 Item4
#>    <fct>       <chr> <chr> <int> <int> <int> <int>
#>  1 P1          D1    1         6     2     3     5
#>  2 P1          D2    1         1     2    NA    NA
#>  3 P2          D1    1         4     9     3     6
#>  4 P2          D2    1         4     1    NA    NA
#>  5 P3          D1    1         7     4     2     8
#>  6 P3          D2    1         1     1    NA    NA
#>  7 P4          D1    1         1     5     1     1
#>  8 P4          D2    1         6     7    NA    NA
#>  9 P5          D1    1         2     0     8     2
#> 10 P5          D2    1         1     4    NA    NA

数据:

df <- structure(list(Participant = structure(1:5, .Label = c("P1", 
"P2", "P3", "P4", "P5"), class = "factor"), D1_1_1 = c(6L, 4L, 
7L, 1L, 2L), D1_1_2 = c(2L, 9L, 4L, 5L, 0L), D1_1_3 = c(3L, 3L, 
2L, 1L, 8L), D1_1_4 = c(5L, 6L, 8L, 1L, 2L), D2_1_1 = c(1L, 4L, 
1L, 6L, 1L), D2_1_2 = c(2L, 1L, 1L, 7L, 4L)), class = "data.frame", 
row.names = c(NA, -5L))

答案 1 :(得分:0)

这是pivot_longer + pivot_wider的一种方式

library(dplyr)
library(tidyr)

pivot_longer(df, cols = -Participant, names_to = c("Day", "Time", "Item"), 
                 names_pattern = "D(\\d+)_(\\d+)_(\\d+)") %>%
    mutate(Item = paste0("Item",Item)) %>%
    pivot_wider(names_from = Item, values_from = value)

# A tibble: 10 x 7
#   Participant Day   Time  Item1 Item2 Item3 Item4
#   <fct>       <chr> <chr> <int> <int> <int> <int>
# 1 P1          1     1         6     2     3     5
# 2 P1          2     1         1     2    NA    NA
# 3 P2          1     1         4     9     3     6
# 4 P2          2     1         4     1    NA    NA
# 5 P3          1     1         7     4     2     8
# 6 P3          2     1         1     1    NA    NA
# 7 P4          1     1         1     5     1     1
# 8 P4          2     1         6     7    NA    NA
# 9 P5          1     1         2     0     8     2
#10 P5          2     1         1     4    NA    NA

我们还可以使用extract,其使用方式与names_pattern中的pivot_longer

pivot_longer(df, cols = -Participant) %>%
     extract(name, into = c("Day", "Time", "Item"), 
             regex = "D(\\d+)_(\\d+)_(\\d+)") %>%
     pivot_wider(names_from = Item, values_from = value)

数据

df <- structure(list(Participant = structure(1:5, .Label = c("P1", 
"P2", "P3", "P4", "P5"), class = "factor"), D1_1_1 = c(6L, 4L, 
7L, 1L, 2L), D1_1_2 = c(2L, 9L, 4L, 5L, 0L), D1_1_3 = c(3L, 3L, 
2L, 1L, 8L), D1_1_4 = c(5L, 6L, 8L, 1L, 2L), D2_1_1 = c(1L, 4L, 
1L, 6L, 1L), D2_1_2 = c(2L, 1L, 1L, 7L, 4L)), class = "data.frame", 
row.names = c(NA, -5L))