加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

可以使用没有聚合功能的dcast吗?

发布时间:2020-12-14 04:30:21 所属栏目:百科 来源:网络整理
导读:Possible Duplicate: 07000 来自reshape2的dcast没有公式,没有重复。拿这些示例数据: df - structure(list(id = c("A","B","C","A","C"),cat = c("SS","SS","SV","SV"),val = c(220L,222L,223L,224L,225L,2206L)),.Names = c("id","cat","val"),class = "d

Possible Duplicate:
07000

来自reshape2的dcast没有公式,没有重复。拿这些示例数据:

df <- structure(list(id = c("A","B","C","A","C"),cat = c("SS","SS","SV","SV"),val = c(220L,222L,223L,224L,225L,2206L)),.Names = c("id","cat","val"),class = "data.frame",row.names = c(NA,-6L))

我想dcast这些数据,只是将值列表,而不对value.var应用任何函数,包括默认长度。

在这种情况下,它工作正常。

> dcast(df,id~cat,value.var="val")
  id  SS   SV
1  A 220  224
2  B 222  225
3  C 223 2206

但是当有重复的变量时,乐趣默认为长度。有办法避免吗?

df2 <- structure(list(id = c("A",220L,1L)),-7L))

> dcast(df2,value.var="val")
Aggregation function missing: defaulting to length
  id SS SV
1  A  1  1
2  B  1  1
3  C  1  2

理想情况下,我正在寻找的是添加一个fun = NA,因为不要尝试聚合value.var。 df2中我想要的结果df2:

id  SS  SV
1  A 220 224
2  B 222 225
3  C 223 220
4. C NA  1
我不认为有办法直接做,但我们可以添加一个额外的列,这将有助于我们
df2 <- structure(list(id = c("A",-7L))

library(reshape2)
library(plyr)
# Add a variable for how many times the id*cat combination has occured
tmp <- ddply(df2,.(id,cat),transform,newid = paste(id,seq_along(cat)))
# Aggregate using this newid and toss in the id so we don't lose it
out <- dcast(tmp,id + newid ~ cat,value.var = "val")
# Remove newid if we want
out <- out[,-which(colnames(out) == "newid")]
> out
#  id  SS  SV
#1  A 220 224
#2  B 222 225
#3  C 223 220
#4  C  NA   1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读