【发布时间】:2016-02-02 22:20:12
【问题描述】:
我希望能够在表格中按订单号比较差异,并附上一列说明差异。例如我想要这个
order color type shape alert
1 1 blue a circle type
2 1 blue b circle
3 2 green a circle color
4 2 blue a circle color type shape
5 2 yellow b triangle type
6 2 yellow c triangle
7 3 orange c triangle
看起来像这样
order color type shape alert
1 1 blue a circle type
2 1 blue b circle
3 2 green a circle color type shape
4 2 blue a circle
5 2 yellow b triangle
6 2 yellow c triangle
7 3 orange c triangle
我的代码只比较相邻的 2 行我如何有效地比较具有相同订单号的所有行?我可以避免循环吗?这是我的代码
order = c(0001, 0001, 0002, 0002, 0002, 0002, 0003)
color = c("blue", "blue", "green", "blue", "yellow", "yellow", "orange")
type = c("a", "b", "a", "a", "b", "c", "c")
shape = c("circle", "circle", "circle", "circle", "triangle", "triangle", "triangle")
df = data.frame(order, color, type, shape)
df$alert <- ""
for(i in 1:nrow(df)-1){
if(identical(df$order[i+1],df$order[i])){
if(!identical(df$color[i+1],df$color[i])){
df$alert[i] <- paste(df$alert[i],"color")
}
if(!identical(df$type[i+1],df$type[i])){
df$alert[i] <- paste(df$alert[i],"type")
}
if(!identical(df$shape[i+1],df$shape[i])){
df$alert[i] <- paste(df$alert[i],"shape")
}
}
}
【问题讨论】:
标签: r dataframe duplicates unique