[英]Reshaping Data Wide To Long: New variables based on Column Names
我想将数据集从宽格式重塑为长格式。
数据集包含 300 个变量,每个变量都以原理命名:ModelID_Emotion_ModelGender。 下面的示例数据:
df <- structure(list(X71_Anger_Male = structure(c(3L, 1L, 2L), .Label = c("Anger",
"Disgust", "Fear"), class = "factor"), X71_Disgus_Male = structure(c(2L,
1L, 1L), .Label = c("Disgust", "Fear"), class = "factor")), class = "data.frame", row.names = c(NA,
-3L))
看起来像
X71_Anger_Male X71_Disgus_Male
1 Fear Fear
2 Anger Disgust
3 Disgust Disgust
我想以将列名中的信息获取并放入新变量的方式转置数据。 例如,应该有一个新变量ModelGender,一个新变量modelID和一个新变量emotion。 所以数据集应该是这样的:
desired <- structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Male", class = "factor"),
ModelNumber = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "X71", class = "factor"),
Emotion = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label = c("Anger",
"Disgust"), class = "factor"), Response = structure(c(3L,
2L, 2L, 3L, 1L, 2L), .Label = c("Anger", "Disgust", "Fear"
), class = "factor")), class = "data.frame", row.names = c(NA,
-6L))
这应该看起来像
Gender ModelNumber Emotion Response
1 Male X71 Disgust Fear
2 Male X71 Disgust Disgust
3 Male X71 Disgust Disgust
4 Male X71 Anger Fear
5 Male X71 Anger Anger
6 Male X71 Anger Disgust
当我使用重塑或聚集/扩散或熔化/铸造时,它不会给出预期的结果。 有谁知道如何做到这一点?
感谢您的时间!
您可以简单地转换为 long 并拆分您想要的列。 通过 tidyverse 方法的一种方法可以是,
library(dplyr)
library(tidyr)
df %>%
pivot_longer(everything()) %>%
separate(name, into = c('ModelNumber', 'Emotion', 'Gender'), sep = '_')
在pivot_longer
,您可以将names_sep
指定为"_"
并将列名拆分为 3 列。
tidyr::pivot_longer(df, cols = everything(),
names_to = c('ModelNumber', 'Emotion', 'Gender'),
values_to = 'Response',
names_sep = '_')
# A tibble: 6 x 4
# ModelNumber Emotion Gender Response
# <chr> <chr> <chr> <fct>
#1 X71 Anger Male Fear
#2 X71 Disgus Male Fear
#3 X71 Anger Male Anger
#4 X71 Disgus Male Disgust
#5 X71 Anger Male Disgust
#6 X71 Disgus Male Disgust
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.