从宽到长与 R pivot_longer() 有两个内部因素存储在一个变量中

from wide to long with R pivot_longer() with two within-factors stored in one variable

我想计算具有两个主题内因素的方差分析,为此我首先需要将文件格式从宽格式转换为长格式。这里的变量名包括2个不同的内部因素:3因素变量lureType(new,sem,per)和2因素变量emotion(Neu,Neg)其值代表因变量(falseAlarms)。

所以,我从选择相关变量开始:

long_file <- wide_file %>%
  select(ID, betweenVariable, newNeu_falseAlarm,newNeg_falseAlarm,
semNeu_falseAlarm,semNeg_falseAlarm,
perNeu_falseAlarm,perNeg_falseAlarm) %>% 

如果变量只包含一个因子,我会遵循这个 对于情感:

 pivot_longer(cols = c(Neg_falseAlarm, Neu_falseAlarm),
               names_to = "emotion",
               values_to = "falseAlarms")

或对于 lureType:

pivot_longer(cols = c(new_falseAlarm, sem_falseAlarm, per_falseAlarm),
               names_to = "lureType",
               values_to = "falseAlarms")

如果变量存储两个不同的因子,有人知道如何将长格式转换为宽格式吗?

TIA!

我找到了解决方案:

pivot_longer(cols=c(newNeu_falseAlarm,newNeg_falseAlarm, semNeu_falseAlarmsemNeg_falseAlarm perNeu_falseAlarm,perNeg_falseAlarm),
               names_to=c("lureType","emotion"),
               names_sep= "N",
               values_to = "falseAlarms"
               )

很高兴,如果有人想添加更优雅的解决方案。我对R不是很熟悉