使用 pivot_longer 将水平格式重塑为长格式 [英] Reshape horizontal to to long format using pivot_longer

查看:71
本文介绍了使用 pivot_longer 将水平格式重塑为长格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用之前提供的相同代码将我的数据改造成长格式而不是宽格式

这是使用的代码:

library(dplyr);library(tidyr);library(stringr)out <- data %>% pivot_longer(cols = -Serial_ID,names_to = c(".value", "prevspost"),names_pattern = "(.*)_(pre|post.*)",names_sep=_") #%>% as.data.frame

我也试过 names_prefix = c("pre_","post_") 而不是 names_pattern = "(.*)_(pre|post.*)";, 但它不起作用.

任何建议将不胜感激.

解决方案

Edit 我推荐使用 @Dave2e 的 优越的方法.

您的尝试无效的原因是模式必须按顺序匹配.你可以试试这个:

库(tidyr)图书馆(dplyr)数据1%>%pivot_longer(cols = -Serial_ID,names_to = c("prevspost",".value"),names_pattern = "(pre|post)_(\\w+)") %>%dplyr::arrange(desc(prevspost),Serial_ID)# 小块:20 x 5Serial_ID prevspost EDV ESV LVEF<int><chr><dbl><dbl><dbl>1 1 前 76.2 32.9 56.82 2 前 65.4 35.9 45.13 3 前 64.4 35.1 45.54 4 前 50 13.9 72.15 5 前 89.6 32 64.36 6 前 62 20.6 66.77 7 前 91.2 37.7 58.68 8 前 62 24 61.39 9 前 104.22.7 78.810 10 前 90.6 31.2 65.611 1 个帖子 86.3 36.6 57.612 2 柱 60.1 26.1 56.713 3 后 72.5 41.1 43.314 4 柱 46.4 18.4 60.415 5 后 70.9 19.3 72.816 6 岗位 55.9 17.8 68.217 7 后 61.9 23.8 61.618 8 后 69.3 34.9 49.619 9 后 38.6 11.5 70.120 10 后 48 16.1 66.4

I am trying to reshape my data to long instead of wide format using the same code provided earlier link , however it doesn't work even after several trials to modify names_pattern = "(.*)_(pre|post.*)",

My data sample is

data1<-read.table(text="
Serial_ID   pre_EDV pre_ESV pre_LVEF    post_EDV    post_ESV    post_LVEF
1   76.2    32.9    56.8    86.3    36.6    57.6
2   65.4    35.9    45.1    60.1    26.1    56.7
3   64.4    35.1    45.5    72.5    41.1    43.3
4   50      13.9    72.1    46.4    18.4    60.4
5   89.6    32      64.3    70.9    19.3    72.8
6   62      20.6    66.7    55.9    17.8    68.2
7   91.2    37.7    58.6    61.9    23.8    61.6
8   62      24      61.3    69.3    34.9    49.6
9   104.1   22.7    78.8    38.6    11.5    70.1
10  90.6    31.2    65.6    48      16.1    66.4", sep="", header=T)

I want to reshape my data to

  1. put identical column headings below each other eg post_EDV below pre_EDV
  2. Create new column Pre vs. post
  3. Fix column heading (remove "pre_" and "post_" to be "EDV" only (as shown in the screenshot below)).

This is the used code:

library(dplyr);library(tidyr);library(stringr)
out <- data %>% pivot_longer(cols = -Serial_ID, 
           names_to = c(".value", "prevspost"), 
           names_pattern =  "(.*)_(pre|post.*)",
           names_sep="_") #%>% as.data.frame

Also I tried names_prefix = c("pre_","post_") instead of names_pattern = "(.*)_(pre|post.*)", but it doesn't work.

Any advice will be greatly appreciated.

解决方案

Edit I recommend using @Dave2e's superior approach.

The reason your attempt didn't work is because the pattern has to match in order. You could try this:

library(tidyr)
library(dplyr) 
data1 %>% pivot_longer(cols = -Serial_ID, 
           names_to = c("prevspost",".value"), 
           names_pattern =  "(pre|post)_(\\w+)") %>%
   dplyr::arrange(desc(prevspost),Serial_ID)
# A tibble: 20 x 5
   Serial_ID prevspost   EDV   ESV  LVEF
       <int> <chr>       <dbl> <dbl> <dbl>
 1         1 pre          76.2  32.9  56.8
 2         2 pre          65.4  35.9  45.1
 3         3 pre          64.4  35.1  45.5
 4         4 pre          50    13.9  72.1
 5         5 pre          89.6  32    64.3
 6         6 pre          62    20.6  66.7
 7         7 pre          91.2  37.7  58.6
 8         8 pre          62    24    61.3
 9         9 pre         104.   22.7  78.8
10        10 pre          90.6  31.2  65.6
11         1 post         86.3  36.6  57.6
12         2 post         60.1  26.1  56.7
13         3 post         72.5  41.1  43.3
14         4 post         46.4  18.4  60.4
15         5 post         70.9  19.3  72.8
16         6 post         55.9  17.8  68.2
17         7 post         61.9  23.8  61.6
18         8 post         69.3  34.9  49.6
19         9 post         38.6  11.5  70.1
20        10 post         48    16.1  66.4

这篇关于使用 pivot_longer 将水平格式重塑为长格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆