[英]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.