2019-01-31
阅读量:
1960
R语言中如何判断并删除合并相同行
问题描述:
某两行除了一个属性A不同,其他属性全部相同。某两行除了一个属性B不同,其他全部相同,该如何合并呢?
由于是用shiny读取excel并进行相关操作,可能有的excel只有属性A重复,有的只有属性B重复,有的A和B都重复。
(注:只有SITE和Kind会出现重复,且ID是唯一识别标识)
解决方法:
##define data frame
dat <- data.frame(ID=c(8,8,4,5,9,9),
SITE=c('beijing','shanghai','beijing','beijing','shanghai','shanghai'),
Kind=c(rep('baby',5),'beauty'),
Volume=c(1,1,4,5,2,2))
dat
dat.new <- by(data = dat,INDICES = dat$ID,FUN = function(x){
site.new <- paste(unique(x$SITE),collapse = ',')
kind.new <- paste(unique(x$Kind),collapse = ',')
dat2 <- x[1,]
dat2$SITE <- site.new
dat2$Kind <- kind.new
dat2
})
dat.new <- dat.new[as.character(unique(dat$ID))]
dat.new <- do.call(rbind,dat.new)
rownames(dat.new) <- NULL
dat.new






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
1条评论