简体   繁体   English

用于在 R 中从宽到长的数据帧中的多列

[英]for multiple columns in data frame reshaping from wide to long in R

I need to reshape my data (a subset as below) from wide to long format.我需要将我的数据(如下子集)从宽格式重塑为长格式。 I have tried reshape() and pivot_longer() , but I could not make what I need.我已经尝试过reshape()pivot_longer() ,但我无法做出我需要的东西。 I have added the part of output of long format I would like to make.我已经添加了我想要制作的长格式 output 的部分。 I really appreciate your help.我真的很感谢你的帮助。

structure(list(id = c(1, 2, 3, 4), g6m = c("12", "11", "16.9", 
"17.1"), g12m = c("18", "11", "11.1", "19.5"), g18m = c("29", 
"11.6", "12.3", "17"), g24m = c("12", "13.6", "10", "11"), age = c(45, 
37, 51, 47), gender = c(0, 1, 0, 1), ms = c("11.136", "12.18", 
"19.01", "10.01")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-4L))

在此处输入图像描述

You could do:你可以这样做:

library(tidyverse)

pivot_longer(df, matches("g\\d+m"), values_to = "g", names_to = "time") %>%
  mutate(time = match(time, colnames(df)) - 1)
#> # A tibble: 16 x 6
#>       id   age gender ms      time g    
#>    <dbl> <dbl>  <dbl> <chr>  <dbl> <chr>
#>  1     1    45      0 11.136     1 12   
#>  2     1    45      0 11.136     2 18   
#>  3     1    45      0 11.136     3 29   
#>  4     1    45      0 11.136     4 12   
#>  5     2    37      1 12.18      1 11   
#>  6     2    37      1 12.18      2 11   
#>  7     2    37      1 12.18      3 11.6 
#>  8     2    37      1 12.18      4 13.6 
#>  9     3    51      0 19.01      1 16.9 
#> 10     3    51      0 19.01      2 11.1 
#> 11     3    51      0 19.01      3 12.3 
#> 12     3    51      0 19.01      4 10   
#> 13     4    47      1 10.01      1 17.1 
#> 14     4    47      1 10.01      2 19.5 
#> 15     4    47      1 10.01      3 17   
#> 16     4    47      1 10.01      4 11   

In principle same as @Allan Cameron.原则上与@Allan Cameron 相同。 Just adapted to your desired output:刚刚适应您想要的 output:

library(tidyr)
library(dplyr)
df %>% 
  pivot_longer(
    cols = -c(id, age, gender, ms),
    names_to = "x",
    values_to = "g"
  ) %>% 
  group_by(id) %>% 
  mutate(time = row_number(), .before=2) %>% 
  select(id, time, g, age, gender, ms)

    id  time g       age gender ms    
   <dbl> <int> <chr> <dbl>  <dbl> <chr> 
 1     1     1 12       45      0 11.136
 2     1     2 18       45      0 11.136
 3     1     3 29       45      0 11.136
 4     1     4 12       45      0 11.136
 5     2     1 11       37      1 12.18 
 6     2     2 11       37      1 12.18 
 7     2     3 11.6     37      1 12.18 
 8     2     4 13.6     37      1 12.18 
 9     3     1 16.9     51      0 19.01 
10     3     2 11.1     51      0 19.01 
11     3     3 12.3     51      0 19.01 
12     3     4 10       51      0 19.01 
13     4     1 17.1     47      1 10.01 
14     4     2 19.5     47      1 10.01 
15     4     3 17       47      1 10.01 
16     4     4 11       47      1 10.01 

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

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