如何在R中的多个值参数上使用collect()函数

时间:2019-06-01 11:11:13

标签: r dataframe tidyr

我是 tidyverse 数据处理的新手,并且我正在使用gather()包中的tidyr函数来将数据的格式从宽格式更改为长格式。
我有以下data数据框:

id <- 1:10
stim_1_ACC <- 0.5:10
stim_2_ACC <- 10:19
stim_1_RT <- 0.4:10
stim_2_RT <- 15:24
data <- data.frame(id,stim_1_ACC,stim_2_ACC,stim_1_RT,stim_2_RT)

我将有stim的一列,其中有stim1stim2作为值,还有两列ACCRT作为数字变量。
使用gather()函数,我只能选择一个value参数,因此只能对一个变量执行我想做的事情。

data %>%
  gather(key = "Stimuli", value = "ACC", 2:5)

我通过多个步骤来实现我的目标,先拆分然后绑定数据框列,但我正在寻找一种更整洁的方法。最终结果将是这样:

   id   stim  ACC  RT
1   1 stim_1  1.5 900
2   2 stim_1  2.5 901
3   3 stim_1  3.5 902
4   4 stim_1  4.5 903
5   5 stim_1  5.5 904
6   6 stim_2  6.5 905
7   7 stim_2  7.5 906
8   8 stim_2  8.5 907
9   9 stim_2  9.5 908
10 10 stim_2 10.5 909

谢谢!

2 个答案:

答案 0 :(得分:0)

可能,收集后,您需要使用extract / separate来分离"stim..""RT"/"ACC"组件,然后使用spread

library(dplyr)
library(tidyr)

data %>%
  gather(key, value, -id) %>%
  extract(key, into = c("stim", "temp"), regex = "(stim_\\d+)_(.*)") %>%
  spread(temp, value)

答案 1 :(得分:0)

这里是separate的一个选项,可通过在字符元素之前的'_'处将'key'列分为'stim'和'temp'

library(tidyverse)
data %>% 
   gather(key, value, -id) %>% 
   separate(key, into = c("stim", "temp"), sep="(_)(?=[A-Z])") %>%
   spread(temp, value)