1

R ID=Custid の次のデータセットを使用

ID Geo Channel Brand Neworstream RevQ112 RevQ212 RevQ312
1  NA  On-line  1      New         5         0       1
1  NA  On-line  1      Stream      5         0       1
3  EU  Tele     2       Stream     5         1       0

データセットをこの形式の列に変換したい

ID Geo Brand Neworstream OnlineRevQ112 TeleRevQ112 OnlineRevQ212 TeleRevQ212

これを行う最善の方法は何ですか?R で最適なコマンドがわかりません。

前もって感謝します

4

2 に答える 2

4

reshape2パッケージとそのmelt関数dcastを使用して、データを再構築できます。

data <- structure(list(ID = c(1L, 1L, 3L), Geo = structure(c(NA, NA, 
1L), .Label = "EU", class = "factor"), Channel = structure(c(1L, 
1L, 2L), .Label = c("On-line", "Tele"), class = "factor"), Brand = c(1L, 
1L, 2L), Neworstream = structure(c(1L, 2L, 2L), .Label = c("New", 
"Stream"), class = "factor"), RevQ112 = c(5L, 5L, 5L), RevQ212 = c(0L, 
0L, 1L), RevQ312 = c(1L, 1L, 0L)), .Names = c("ID", "Geo", "Channel", 
"Brand", "Neworstream", "RevQ112", "RevQ212", "RevQ312"), class = "data.frame", row.names = c(NA, 
-3L)) 

library(reshape2)
## melt data
df_long<-melt(data,id.vars=c("ID","Geo","Channel","Brand","Neworstream"))

## recast in combinations of channel and time frame
dcast(df_long,... ~Channel+variable,sum)
于 2013-08-09T17:51:14.017 に答える