使用部分字符串创建新变量(重新发布)

Using partial strings to create new variables (repost)

我希望我能就如何在 R 中将我的数据转换为长格式向您征求意见。

我想从数据框中的变量名称中分离出条件、发色团和源检测器。下面我粘贴了几个示例(但数据框中还有更多变量)。所以我需要挑选出“愤怒”和“快乐”并将它们放入变量“条件”中,然后是发色团 HboHbR、和 Hbt,最后是源检测器对“1,1”、“1,2”(可能搜索和拉出逗号前后的文本,因为我有 49 种这些组合).

示例变量名称:

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

感谢您的帮助!

一切顺利, 卡罗琳

我做了一个和你上面描述的数据类似的数据

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

这是解决问题的方法

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 

感谢您的帮助!我能够使用它来得出我需要的结论。见下文:

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"))