繁体   English   中英

如何将面板数据从宽更改为长,以及如何创建时间变量并添加它

[英]How do I change my panel data from wide to long and also, how do I create a time variable and add it

我在让我的代码工作时遇到了一些麻烦。 我之前在这个网站上问过一个问题,但并没有完全解决我的问题。 “从宽到长重塑 HRS 数据并创建时间变量”

这一次,我试图非常清楚和准确地描述我的数据。 它看起来像这样,其中所有变量都以“r”开头,后跟从 1 到 10 的数字,后跟测量的变量。 唯一不以“r”开头的变量是称为“idhhpn”的id-tracker。

这是我的数据结构示例,但不完全是我的数据。 我的数据文件非常大,无法在此处发布:

df <- structure(list(data = structure(1:4, .Label = c("Ind_1", "Ind_2", 
    "Ind_3", "Ind_4"), class = "factor"), r1weight = c(56, 76, 87, 64
    ),r10weight = c(57, 75, 88, 66), r1height = c(186, 176, 187, 165), r10height = c(187L, 
    173L, 185L, NA), r1bmi = c(23L, 22L, 25L, 21L), r10bmi = c(24L, 23L, 
    29L, 23), r1logass = c(8L, 4L, NA, 2L), r10logass = c(7, 5L, 2, 
    4L), r1vigact = c(1, 0, 1, 1), r10vigact = c(0,0,0,1), idhhpn = c(1,2,3,4), rmale = c(0,0,1,0), rhighs = c(1,1,1,0), rcoll = c(1,0,1,0) ), class = 
    "data.frame", row.names = c(NA, 
    -4L)) 



data       r1weight r10weight r1height r10height r1bmi r10bmi r1logass r10logass r1vigact r10vigact idhhpn rmale rhighs rcoll
1 Ind_1       56        57      186       187    23     24        8         7        1         0      1     0      1     1
2 Ind_2       76        75      176       173    22     23        4         5        0         0      2     0      1     0
3 Ind_3       87        88      187       185    25     29       NA         2        1         0      3     1      1     1
4 Ind_4       64        66      165        NA    21     23        2         4        1         1      4     0      0     0
`

我有 23 个变量都观察了 10 次(每年一个,持续 10 年)。 我还有几个假人,如rmalerhispanicrblackrHSrGEDrCollege等。

我希望将其转换为:

dflong <- structure(list(time = structure(1:12, .Label = c("1", "...","10","1", "...","10","1", "...","10", "1", "...","10"), 
     class = "factor"), idhhpn = c(1,1,1,2,2,2,3,3,3,4,4,4), W = c(56,"...", 57,76,"...",75,87,"...",88,64,"...",66),
     H = c(186,"...",187,176,"...",173,187,"...",185,165,"...","..."), BMI = c(23,"...",24,22,"...",23,25,"...",29,21,"...",23),
     logA = c(8,"...",7,4,"...",5,"...","...",2,2,"...",4), vigact = c(1,"...",0,0,"...",0,1,"...",0,1,"...",1),
     rmale = c(0,"...",0,0,"...",0,1,"...",1,0,"...",0), rhighs = c(1,"...",1, 1,"...",1,1, "...",1,0,"...",0),
     rcoll = c(1,"...",1,0,"...",0,1,"...",1,0,"...",0)),
     class = "data.frame", row.names = c(NA, -12L))`

   time idhhpn W   H BMI logA vigact rmale rhighs rcoll
1     1      1  56 186  23    8      1     0      1     1
2   ...      1 ... ... ...  ...    ...   ...    ...   ...
3    10      1  57 187  24    7      0     0      1     1
4     1      2  76 176  22    4      0     0      1     0
5   ...      2 ... ... ...  ...    ...   ...    ...   ...
6    10      2  75 173  23    5      0     0      1     0
7     1      3  87 187  25  ...      1     1      1     1
8   ...      3 ... ... ...  ...    ...   ...    ...   ...
9    10      3  88 185  29    2      0     1      1     1
10    1      4  64 165  21    2      1     0      0     0
11  ...      4 ... ... ...  ...    ...   ...    ...   ...
12   10      4  66 ...  23    4      1     0      0     0

对于每个变量,每个人还有一个从 1 到 10 的时间变量,如图所示。

我省略了时间戳 2-9(为了可读性)

我目前有以下代码,我确信它几乎是正确的。

HRSdata_melt <- HRSdata %>% gather(time,ind,-HRSdata) %>%
                    mutate(time=gsub("r([1-10])", "\\1_",time)) %>%
                    separate(time, into = c("time", "idhhpn")) %>% 
                    spread(idhhpn, ind)

但它给了我以下错误,我认为这是由于一些小错误造成的。

在此处输入图片说明

在此处输入图片说明

这是dput(head(HRSdata))的示例在此处输入图片说明

我们可以使用dplyr::matchesgather所有列,其中 r 后跟一个数字或更多,后跟任何东西,即r1weightr2weight 、 ... 、 r10weight等。然后mutatespread

library(dplyr)
library(tidyr)
df %>% gather(key,val,matches('r\\d+.*')) %>% 
       mutate(time=gsub('r(\\d+).*','\\1',key), key=sub('(r\\d+)(.*)','\\2',key)) %>%
       spread(key,val)

  • r(\\\\d+).*r之后获取一个或多个数字作为第一组并使用\\\\1返回该数字

  • (r\\\\d+)(.*)(r\\\\d+)之后获取任何内容作为第二组并使用\\\\2返回

  • 暂无
    暂无

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM