R 如何将长排序订单数据重塑为宽数据格式
数据片段取自mlogit包(Game2),它采用长格式来模拟我的情况。其中ch为平台的等级,chid为一个响应者的idR 如何将长排序订单数据重塑为宽数据格式,r,reshape,reshape2,R,Reshape,Reshape2,数据片段取自mlogit包(Game2),它采用长格式来模拟我的情况。其中ch为平台的等级,chid为一个响应者的id age hours platform ch own chid 1 33 2.00 GameBoy 6 0 1 2 33 2.00 GameCube 5
age hours platform ch own chid
1 33 2.00 GameBoy 6 0 1
2 33 2.00 GameCube 5 0 1
3 33 2.00 PC 4 1 1
4 33 2.00 PlayStation 1 1 1
5 33 2.00 PSPortable 3 0 1
6 33 2.00 Xbox 2 0 1
7 19 3.25 GameBoy 6 0 2
8 19 3.25 GameCube 5 0 2
9 19 3.25 PC 1 1 2
10 19 3.25 PlayStation 2 1 2
11 19 3.25 PSPortable 3 0 2
12 19 3.25 Xbox 4 0 2
13 18 4.00 GameBoy 6 0 3
14 18 4.00 GameCube 4 0 3
15 18 4.00 PC 5 1 3
16 18 4.00 PlayStation 1 1 3
17 18 4.00 PSPortable 2 0 3
18 18 4.00 Xbox 3 0 3
我需要将这些长数据转换为宽格式,如下所示。它在mlogit包中。排名保留(从第1列(即ch.Xbox)到第6列(即ch.PC)
我的问题是将上面给出的长格式保留为宽格式作为一个示例。我们可以使用
dplyr
和tidyr
执行重塑
library(dplyr)
library(tidyr)
# Reshape the data
dt2 <- dt %>%
gather(type, value, ch, own) %>%
unite("platform_type", type, platform, sep = ".") %>%
spread(platform_type, value) %>%
arrange(chid)
库(dplyr)
图书馆(tidyr)
#重塑数据
dt2%
聚集(类型、值、通道、自身)%>%
联合(“平台类型”,类型,平台,sep=“.”)%>%
排列(平台类型,值)%>%
安排(chid)
如果希望最终输出与所需输出相同,则可以进一步准备列名向量并基于此选择列
# Prepare the column vector
vec <- c("Xbox", "PlayStation", "PSPortable", "GameCube", "GameBoy", "PC")
colname <- unlist(lapply(c("ch.", "own."), function(x) paste0(x, vec)))
colname2 <- c(colname, "age", "hours")
# Select columns
dt3 <- dt2 %>% select(colname2)
# View the result
ch.Xbox ch.PlayStation ch.PSPortable ch.GameCube ch.GameBoy ch.PC own.Xbox own.PlayStation own.PSPortable own.GameCube own.GameBoy own.PC age hours
1 2 1 3 5 6 4 0 1 0 0 0 1 33 2.00
2 4 2 3 5 6 1 0 1 0 0 0 1 19 3.25
3 3 1 2 4 6 5 0 1 0 0 0 1 18 4.00
#准备列向量
vec我们可以使用dplyr
和tidyr
执行整形
library(dplyr)
library(tidyr)
# Reshape the data
dt2 <- dt %>%
gather(type, value, ch, own) %>%
unite("platform_type", type, platform, sep = ".") %>%
spread(platform_type, value) %>%
arrange(chid)
库(dplyr)
图书馆(tidyr)
#重塑数据
dt2%
聚集(类型、值、通道、自身)%>%
联合(“平台类型”,类型,平台,sep=“.”)%>%
排列(平台类型,值)%>%
安排(chid)
如果希望最终输出与所需输出相同,则可以进一步准备列名向量并基于此选择列
# Prepare the column vector
vec <- c("Xbox", "PlayStation", "PSPortable", "GameCube", "GameBoy", "PC")
colname <- unlist(lapply(c("ch.", "own."), function(x) paste0(x, vec)))
colname2 <- c(colname, "age", "hours")
# Select columns
dt3 <- dt2 %>% select(colname2)
# View the result
ch.Xbox ch.PlayStation ch.PSPortable ch.GameCube ch.GameBoy ch.PC own.Xbox own.PlayStation own.PSPortable own.GameCube own.GameBoy own.PC age hours
1 2 1 3 5 6 4 0 1 0 0 0 1 33 2.00
2 4 2 3 5 6 1 0 1 0 0 0 1 19 3.25
3 3 1 2 4 6 5 0 1 0 0 0 1 18 4.00
#准备列向量
可能重复的可能重复的可能重复的