简体   繁体   中英

How to use gather() function for multiple value arguments in R

I am new in the tidyverse data manipulation and I am using gather() function from the tidyr package for changing from wide to long form on my data.
I have the following data dataframe:

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)

I would have one column for stim in which I have stim1 and stim2 as values, and two columns ACC and RT as numeric variables.
With gather() function I can select only one value argument and so having doing what I want only for one variable.

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

I reach my goal with multiple steps, splitting and then binding dataframe columns, but I'm looking for a more tidy approach. Final results would be like this:

   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

Thanks!

Probably , after gathering you'll need to use extract / separate to separate "stim.." and "RT"/"ACC" component and then use spread

library(dplyr)
library(tidyr)

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

Here is an option with separate to split the 'key' column into 'stim' and 'temp' by splitting at the '_' before the character element

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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