我有一个包含X和Y点的大型数据集。我想为其设置动画,所以我想删除固定位置的点。我想删除相同的X和Y重复n次以上的行。
到目前为止,我做到了,还有没有更优雅的解决方案?谢谢!
uniques <- unique(data[c("Lat","Long")])
uniques$values = row.names(uniques)
uniques2 <- inner_join(data,uniques,by=c("Lat","Long"))
reps <- data.frame(unclass(rle(uniques2$values)))
delete <- as.character(reps$values[(reps$lengths)>10])
data2 <- uniques2[! uniques2$values %in% delete),]
答案 0 :(得分:3)
Tidyverse-way是
data2 <- data %>%
group_by(Long, Lat) %>%
filter(n() <= 10) %>%
ungroup()
答案 1 :(得分:1)
使用data.table
,我将尝试这一行解决方案:
library(data.table)
data < as.data.table(data)[, count:=.N, by=.(Lat,Long)][count<n][,count:=NULL]
最好!
答案 2 :(得分:1)
假设您要保留x
和y
开始重复的第一行,则可以使用dplyr
包尝试以下方法:
library(dplyr)
# Example data
df <- data.frame(
x = c(rep(1, 5), 2:6, rep(7, 5)),
y = c(rep(9, 5), 2:6, rep(8, 5))
)
# Cut-off value
n <- 3
# Remove unwanted rows
new_df <- df %>%
mutate(same_as_prev = x == lag(x) & y == lag(y)) %>%
group_by(x, y, same_as_prev) %>%
mutate(consec_count = n()) %>%
filter(consec_count <= n & same_as_prev) %>%
ungroup()