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

问题描述 投票:-1回答:1

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

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

这是我的数据结构的一个示例,但不完全是我的数据。我的数据文件非常大,我不能在这里发布:

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)

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

enter image description here

enter image description here

这是dput(head(HRSdata)) enter image description here的一个例子

r dplyr panel-data
1个回答
0
投票

我们可以在所有列上使用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返回
  • 最新问题
    © www.soinside.com 2019 - 2024. All rights reserved.