简体   繁体   English

r-使用tidyr在多个关键列中收集多个列

[英]r - gather multiple columns in multiple key columns with tidyr

Not sure if tidyr::gather can be used to take multiple columns and merge them in multiple key columns. 不知道tidyr::gather可用于获取多个列并将它们合并到多个键列中。

Similar questions have been asked but they all refer to gathering multiple columns in one key column. 已经提出了类似的问题,但它们都涉及在一个关键列中收集多个列。

I'm trying to gather 4 columns into 2 key and 2 value columns like in the following example: 我正在尝试将4列收集到2个键和2个值列中,如以下示例所示:

Sample data: 样本数据:

df <- data.frame(
    subject = c("a", "b"),
    age1 = c(33, 35),
    age2 = c(43, 45),
    weight1 = c(90, 67),
    weight2 = c(70, 87)
)

  subject age1 age2 weight1 weight2
1       a   33   43      90      70
2       b   35   45      67      87

Desired result: 所需结果:

dfe <- data.frame(
    subject = c("a", "a", "b", "b"),
    age = c("age1", "age2", "age1", "age2"),
    age_values = c(33, 43, 35, 45),
    weight = c("weight1", "weight2", "weight1", "weight2"),
    weight_values = c(90, 70, 67, 87)
)

  subject  age age_values  weight weight_values
1       a age1         33 weight1            90
2       a age2         43 weight2            70
3       b age1         35 weight1            67
4       b age2         45 weight2            87

Here's one approach. 这是一种方法。 The idea is to do the use gather , then split the resulting dataframe by variable (age and weight), do the mutate operations separately for each of the two dataframes, then merge the dataframes back together using subject and the variable number (1 or 2). 想法是进行使用gather ,然后按变量(年龄和权重) split结果数据帧,分别对两个数据帧分别执行mutate操作,然后使用subject和变量编号(1或2)将数据帧合并回去)。

library(dplyr)
library(tidyr)
library(purrr)

df %>%
  gather(age1:weight2, key = key, value = value) %>%
  separate(key, sep = -1, into = c("var", "num")) %>%
  split(.$var) %>%
  map(~mutate(., !!.$var[1] := paste0(var, num), !!paste0(.$var[1], "_values") := value)) %>%
  map(~select(., -var, -value)) %>%
  Reduce(f = merge, x = .) %>%
  select(-num)

Here's one way to do it - 这是一种实现方法-

df %>%
  gather(key = "age", value = "age_values", age1, age2) %>%
  gather(key = "weight", value = "weight_values", weight1, weight2) %>%
  filter(substring(age, 4) == substring(weight, 7))

  subject  age age_values  weight weight_values
1       a age1         33 weight1            90
2       b age1         35 weight1            67
3       a age2         43 weight2            70
4       b age2         45 weight2            87

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

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