简体   繁体   English

如何使用 R 对一个收集列中的多个值列进行整形

[英]How to reshape using R for multiple value columns across one gather column

I want to reshape my data from wide to long using specific columns but create multiple value columns.我想使用特定列将我的数据从宽调整为长,但创建多个值列。 Pasting below an example:在下面粘贴一个示例:

data <- read.table(header=T, text='
 hhid villageid hh_1   hh_2    age_1  age_2
   1   10        ab     pq      10      17
   2   12        cd     rs      11      25
   3   20        ef     tu      8       13
   4   22        gh     vw      9       3
 ')

#the output should gather hh_1:hh_2 to create two value columns

#    hhid villageid    member    name  age
 #     1    10           hh_1     ab    10
 #     1    10           hh_2     pq    17
 #     2    12           hh_1     cd    11
 #     2    12           hh_2     rs    25
 #     3    20           hh_1     ef     8
 #     3    20           hh_2     tu    13
 #     4    22           hh_1     gh     9
 #     4    22           hh_2     vw     3

I have tried dyplyr package function gather without any success.我试过 dyplyr package function gather没有任何成功。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks.谢谢。

Using tidyr::pivot_longer which superseded gather plus some additional data wrangling steps you could do:使用tidyr::pivot_longer gather了 collect 以及一些额外的数据整理步骤,您可以执行以下操作:

library(tidyr)
library(dplyr)

data %>% 
  pivot_longer(-c(hhid, villageid), names_to = c(".value", "member"),
               names_pattern = "(.*)_(.*)") %>% 
  rename(name = "hh") %>% 
  mutate(member = paste("hh", member, sep = "_"))
#> # A tibble: 8 × 5
#>    hhid villageid member name    age
#>   <int>     <int> <chr>  <chr> <int>
#> 1     1        10 hh_1   ab       10
#> 2     1        10 hh_2   pq       17
#> 3     2        12 hh_1   cd       11
#> 4     2        12 hh_2   rs       25
#> 5     3        20 hh_1   ef        8
#> 6     3        20 hh_2   tu       13
#> 7     4        22 hh_1   gh        9
#> 8     4        22 hh_2   vw        3

A base R option using reshape使用reshape的基本 R 选项

reshape(
  data,
  direction = "long",
  idvar = c("hhid","villageid"),
  varying = -(1:2),
  sep = "_"
)

gives

       hhid villageid time hh age
1.10.1    1        10    1 ab  10
2.12.1    2        12    1 cd  11
3.20.1    3        20    1 ef   8
4.22.1    4        22    1 gh   9
1.10.2    1        10    2 pq  17
2.12.2    2        12    2 rs  25
3.20.2    3        20    2 tu  13
4.22.2    4        22    2 vw   3

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

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