r - 旋转更长的语法

标签 r tidyr

我有以下数据格式,看起来非常简单的pivot_longer应该会产生所需的高数据帧,但我没有到达那里。以下是原始数据帧 (HW) 和所需的高数据帧 (HWT) 的简化示例。如何使用pivot_longer()从HW到HWT?也许我需要使用names_pattern而不是names_sep?我玩过这个,但正则表达式对我来说仍然是个谜。

    HW <- tribble(
  ~Subject,  ~Pre.Height,  ~Post1.Height, ~Post2.Height, ~Pre.Weight,  ~Post1.Weight, ~Post2.Weight,
  "A", 110.0, 113.5, 120.0, 18.1, 20.0, 24.4,
  "B", 95.5, 124.5, 129.7, 15.2, 24.6, 27.2,
  "C", 116, 125.8, 136.2, 21, 29.9, 39.9,
  "D", 131, 135.8, 155, 24.3, 26.7, 38.8,
  "E", 145.6, 154, 158.5, 41.3, 48.1, 63.5,
  "F", 121.5, 140, 147.9, 22.8, 36.5, 51.5)
  
HW

HWT <- tribble(
  ~Subject,  ~Time,  ~Height,  ~Weight,
  "A", "Pre", 110.0, 18.1,
  "A", "Post1", 113.5, 20.0, 
  "A", "Post2", 120.0, 24.4,
  "B", "Pre", 95.5, 15.2, 
  "B", "Post1", 124.5, 24.6, 
  "B", "Post2", 129.7, 127.2,
  "C", "Pre", 116, 21,
  "C", "Post1", 125.8,  29.9, 
  "C", "Post2", 136.2, 39.9,
  "D", "Pre", 131, 24.3, 
  "D", "Post1", 135.8, 26.7,
  "D", "Post2", 155, 38.8,
  "E", "Pre", 145.6,  41.3,
  "E", "Post1", 154, 148.1, 
  "E", "Post2", 158.5, 63.5,
  "F", "Pre", 121.5, 122.8, 
  "F", "Post1", 140, 36.5, 
  "F", "Post2", 147.9, 51.5)

# Intuitively I think this should work, names are taken from separator = "Pre", "Post1", "Post2",
# these are store in column Time, associated values parsed and stored in Height/Weight
HW %>% pivot_longer(cols = !Subject,
                    names_sep = ".",
                    names_to = c("Time"),
                    values_to = c("Height", "Weight")
                    )
# Of course this throws an error, I don't believe you can have multiple values_to columns

# Starting simple:
HW %>% pivot_longer(cols = !Subject)


# Tried this but it doesn't help:
HW %>% pivot_longer(cols = !Subject,
                    names_sep = ".",
                    names_to = c("Height", "Weight")
)

最佳答案

两个问题,即 1) 默认情况下,names_sep 将在 regex 模式下解析,即 . 被视为正则表达式中的元字符以匹配任何字符。所以转义它 (\\), 2)。当我们指定 names_sep 时,有两个组件应该在“names_to”中指定,即除了“Time”之外,值组件,即 .value 应该位于“时间”之后,因为时间部分将采用 之前的列名称中的前缀部分。

library(dplyr)
library(tidyr)
HW %>% 
     pivot_longer(cols = -Subject, names_sep = "\\.",
          names_to = c("Time", ".value"))

-输出

# A tibble: 18 x 4
   Subject Time  Height Weight
   <chr>   <chr>  <dbl>  <dbl>
 1 A       Pre    110     18.1
 2 A       Post1  114.    20  
 3 A       Post2  120     24.4
 4 B       Pre     95.5   15.2
 5 B       Post1  124.    24.6
 6 B       Post2  130.    27.2
 7 C       Pre    116     21  
 8 C       Post1  126.    29.9
 9 C       Post2  136.    39.9
10 D       Pre    131     24.3
11 D       Post1  136.    26.7
12 D       Post2  155     38.8
13 E       Pre    146.    41.3
14 E       Post1  154     48.1
15 E       Post2  158.    63.5
16 F       Pre    122.    22.8
17 F       Post1  140     36.5
18 F       Post2  148.    51.5

关于r - 旋转更长的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68867472/

相关文章:

用多列从宽到长 reshape 大型数据集

r - 三个连续变量的等值线图或热图

r - 以半 reshape /半聚合方式清理 data.frame

r - 在 igraph 中存储顶点的顺序

R:使用 rockchalk 或 rgl 再现 3D 绘图

r - 如何在R中使用dplyr获取最近三个月的数据

r - 如何使用 R 按模式识别列并将其转换为日期时间?

r - 如何在R中对这些数据进行排序

r - 在 tidyr::gather 中使用多个键

r - 将数据框折叠成单行并根据行 R 创建新列