簡體   English   中英

用於在 R 中從寬到長的數據幀中的多列

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

我需要將我的數據(如下子集)從寬格式重塑為長格式。 我已經嘗試過reshape()pivot_longer() ,但我無法做出我需要的東西。 我已經添加了我想要制作的長格式 output 的部分。 我真的很感謝你的幫助。

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))

在此處輸入圖像描述

你可以這樣做:

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   

原則上與@Allan Cameron 相同。 剛剛適應您想要的 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