0
votes

I was hoping I could pick your brains as to how to convert my data to long form in R.

I would like to separate out the condition, Chromophore, and source detector from the variable names in my dataframe. Below I pasted a few examples (but there are many more variables in the data frame). So I need to pick out "Angry" and "Happy" and put those into a variable "Condition", then the Chromophores Hbo, HbR, and Hbt, and finally the source detector pairs "1,1", "1,2" (perhaps searching and pulling for the text before and after the comma because I have 49 combinations of these).

Example variable names:

AngryHRFHbO,1,1 AngryHRFHbR,1,1 AngryHRFHbT,1,1 AngryHRFHbO,2,1 AngryHRFHbR,2,1 AngryHRFHbT,2,1 HappyHRFHbO,4,1 HappyHRFHbR,4,1 HappyHRFHbT,4,1 HappyHRFHbO,2,2 HRFHbR,2,2 HappyHRFHbT,2,2

Thank you for your help!

All the best, Caroline

2

2 Answers

0
votes

I made a similar data to your data described as above

library(tidyverse)
library(stringr)
df <- data.frame(obs = 1, "AngryHRFHbO,1,1"  = 1,  "AngryHRFHbR,1,1" = 0, "AngryHRFHbT,1,1" = 3,  "AngryHRFHbO,2,1" = 9, "AngryHRFHbR,2,1" = 0, "HappyHRFHbT,4,1" = 4)
# obs AngryHRFHbO.1.1 AngryHRFHbR.1.1 AngryHRFHbT.1.1 AngryHRFHbO.2.1 AngryHRFHbR.2.1 HappyHRFHbT.4.1
# 1   1               1               0               3               9               0               4

This is an approach you can do to solve your question

df2 <- df %>% 
  pivot_longer(-obs, names_to = "Name", values_to = "Value") %>% 
  mutate(Name = str_replace_all(Name, regex("\\."), ",")) %>%  #you can ignore this line if the variables have the comma
  mutate(Condition = str_extract(Name, regex("Angry|Happy")),
         Chromophore = str_extract(Name, regex("Hbo|HbR|Hbt", ignore_case = TRUE)),
         detector = str_extract(Name, regex("\\d[,]\\d$"))) %>% 
  select(-c(obs, Value))
# Name            Condition Chromophore detector
# <chr>           <chr>     <chr>       <chr>   
# 1 AngryHRFHbO,1,1 Angry     HbO         1,1     
# 2 AngryHRFHbR,1,1 Angry     HbR         1,1     
# 3 AngryHRFHbT,1,1 Angry     HbT         1,1     
# 4 AngryHRFHbO,2,1 Angry     HbO         2,1     
# 5 AngryHRFHbR,2,1 Angry     HbR         2,1     
# 6 HappyHRFHbT,4,1 Happy     HbT         4,1 
0
votes

Thank you for your help! I was able to use this to come to the conclusion I needed. See below:

df2 <- full_data %>% 
pivot_longer(-c("ID", "time"),  names_to = "Name", values_to = "Value") %>% 
mutate(Name = str_replace_all(Name, regex("\\."), "_")) %>%  #you can ignore this line 
if the variables have the comma
mutate(Condition = str_extract(Name, regex("Angry|Happy|Fearful")),
     Chromophore = str_extract(Name, regex("HbO|HbR|HbT", ignore_case = TRUE)),
     Channel = str_extract_all(Name, regex("\\d+[_]\\d+$"))) %>% 
select(c("ID", "time", "Name", "Condition", "Chromophore", "Channel", "Value"))