我需要重新整理下表的格式:
> data
dia cli llam elegidos cumllam
1 1-11 a 1 1 1
2 2-11 a 2 1 3
3 1-11 b 2 1 2
4 2-11 b 1 1 3
5 2-11 c 1 0 1
我需要在行中加入日期和列中的cumllam以及客户端的数量作为值。
我写道:
library(reshape2)
my.f <- function (v) {if (length(v) == 0) 0 else length(v)}
series<-data.frame(dcast(data, dia~cumllam ,
fun.aggregate=my.f, value.var='cli'))
我得到了:
> series
dia X1 X2 X3
1 1-11 1 1 0
2 2-11 1 0 2
但是我需要按照&#34; elegidos&#34;列拆分它。我的桌子应该是:
elegidos dia X1 X2 X3
0 2-11 1 0 0
1 1-11 1 1 0
1 2-11 0 0 2
我试过了:
data.frame(dcast(data, c(elegidos,dia)~cumllam ,
fun.aggregate=my.f, value.var='cli'))
但是我得到了错误的结果:
c.elegidos..dia. X1 X2 X3
1 0 1 0 0
2 1 2 2 2
3 2 1 0 2
我可以过滤第一个表格,然后像第一个代码和rbind一样运行dcast,但我确信只需一步即可完成。
答案 0 :(得分:2)
如果你的目标是问题中的倒数第二个矩阵,你可以使用:
df <- read.table(header=T, text = ' dia cli llam elegidos cumllam
1-11 a 1 1 1
2-11 a 2 1 3
1-11 b 2 1 2
2-11 b 1 1 3
2-11 c 1 0 1
')
require(reshape2)
dcast(df, formula=elegidos + dia ~ cumllam, length)
elegidos dia 1 2 3
1 0 2-11 1 0 0
2 1 1-11 1 1 0
3 1 2-11 0 0 2
答案 1 :(得分:1)
使用基数R:
> reshape(ddf[,-2], idvar=c('dia','elegidos'), timevar='cumllam', direction='wide')
dia elegidos llam.1 llam.3 llam.2
1 1-11 1 1 NA 2
2 2-11 1 NA 2 NA
5 2-11 0 1 NA NA
将NA替换为0:
> rr = reshape(ddf[,-2], idvar=c('dia','elegidos'), timevar='cumllam', direction='wide')
> rr[is.na(rr)] <- 0
> rr
dia elegidos llam.1 llam.3 llam.2
1 1-11 1 1 0 2
2 2-11 1 0 2 0
5 2-11 0 1 0 0