R:删除列中重复元素的行簇

R:删除列中重复元素的行簇,r,dataframe,subset,R,Dataframe,Subset,我有一个有两列的数据帧,其中第二列可以有二进制值(1或2)。我想随机删除第二列始终为2的行簇 例如: df1<-data.frame(x=seq(10),y=c(1,2,2,1,2,2,1,2,1,2)) [1] df1 x y 1 1 1 2 2 2 3 3 2 4 4 1 5 5 2 6 6 2 7 7 1 8 8 2 9 9 1 10 10 2 我尝试过这个,但不起作用: cluster_size=2 percentage_cluste

我有一个有两列的数据帧,其中第二列可以有二进制值(1或2)。我想随机删除第二列始终为2的行簇

例如:

df1<-data.frame(x=seq(10),y=c(1,2,2,1,2,2,1,2,1,2))
[1] df1

    x y
1   1 1
2   2 2
3   3 2
4   4 1
5   5 2
6   6 2
7   7 1
8   8 2
9   9 1
10 10 2
我尝试过这个,但不起作用:

cluster_size=2
percentage_clusters_to_remove= 0.5
clusters<-which(df1$y==rep(cluster_size,2))
remove<-sample(clusters, length(clusters)*percentage_clusters_to_remove)
df2<-df1[-c(remove),]
cluster\u size=2
集群移除百分比=0.5

像这样用
tidyverse

聚集

df1%>%
  slice(-sample(which(y==2), round(length(which(df1$y==2))*0.5,0)) )

不抱歉,这只是删除一行。不产生问题中建议的“欲望输出”
df1%>%
  slice(-sample(which(y==2), round(length(which(df1$y==2))*0.5,0)) )