我想将行组转换为列组。但是我不希望他们彼此重叠。我想要结果彼此相邻。 这是数据:
YEAR,RACE,0years,1years,2years
2016,White,c2,d2,e2
2016,Black,c3,d3,e3
2016,Native,c4,d4,e4
2017,White,c5,d5,e5
2017,Black,c6,d6,e6
2017,Native,c7,d7,e7
2018,White,c8,d8,e8
2018,Black,c9,d9,e9
2018,Native,c10,d10,e10
2019,White,c11,d11,e11
2019,Black,c12,d12,e12
2019,Native,c13,d13,e13
2020,White,c14,d14,e14
2020,Black,c15,d15,e15
2020,Native,c16,d16,e16
我尝试使用excel和数据透视表,但是数据集太大。以上仅是一个汇总示例。低于我预期的结果。我用过R,但是我不知道从哪里开始用这种不同的格式
,0years,0years,0years,1years,1years,1years,2years,2years,2years
,White,Black,Native,White,Black,Native,White,Black,Native
2016,c2,c3,c4,d2,d3,d4,e2,e3,e4
2017,c5,c6,c7,d5,d6,d7,e5,e6,e7
2018,c8,c9,c10,d8,d9,d10,e8,e9,e10
2019,c11,c12,c13,d11,d12,d13,e11,e12,e13
2020,c14,c15,c16,d14,d15,d16,e14,e15,e16
答案 0 :(得分:0)
我们可以使用dcast
中的data.table
,这可能需要多个value.var
library(data.table)
dcast(setDT(df1), YEAR ~ RACE, value.var = c("0years", "1years", "2years"))
# YEAR 0years_Black 0years_Native 0years_White 1years_Black 1years_Native 1years_White 2years_Black 2years_Native 2years_White
#1: 2016 c3 c4 c2 d3 d4 d2 e3 e4 e2
#2: 2017 c6 c7 c5 d6 d7 d5 e6 e7 e5
#3: 2018 c9 c10 c8 d9 d10 d8 e9 e10 e8
#4: 2019 c12 c13 c11 d12 d13 d11 e12 e13 e11
#5: 2020 c15 c16 c14 d15 d16 d14 e15 e16 e14
df1 <- structure(list(YEAR = c(2016L, 2016L, 2016L, 2017L, 2017L, 2017L,
2018L, 2018L, 2018L, 2019L, 2019L, 2019L, 2020L, 2020L, 2020L
), RACE = c("White", "Black", "Native", "White", "Black", "Native",
"White", "Black", "Native", "White", "Black", "Native", "White",
"Black", "Native"), `0years` = c("c2", "c3", "c4", "c5", "c6",
"c7", "c8", "c9", "c10", "c11", "c12", "c13", "c14", "c15", "c16"
), `1years` = c("d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9",
"d10", "d11", "d12", "d13", "d14", "d15", "d16"), `2years` = c("e2",
"e3", "e4", "e5", "e6", "e7", "e8", "e9", "e10", "e11", "e12",
"e13", "e14", "e15", "e16")), class = "data.frame", row.names = c(NA,
-15L))
答案 1 :(得分:0)
这里是tidyr选项。
library(tidyverse)
df1 %>%
gather(key, value, -(RACE:YEAR)) %>%
unite(new_col, key, RACE, sep = "_", remove=T) %>%
spread(new_col, value)