思想:將讀取的數據當做數據庫中的數據表,讀取的數據放置到數據內存中臨時存儲,以SQL語句對數據進行篩選,得出想要的數據內容。
數據情況
篩選V2和V3兩列中,元素的重複次數超過2次以上的數據,其中2和3的重複次數超過3次,需要篩選出來。
篩選數據R語言程序
library(sqldf)
data <- read.csv("data.csv")
head(data)
zdata <- sqldf("select * from data t where (select count(1) from data where V2 = t.V2 and V3 = t.V3)>2")
print(zdata)
使用的SQL語句爲:
select * from data t where (select count(1) from data where V2=t.V2 and V3=t.V3)>2
得到曬選的效果如下所示:
方法二
v1=c(1,21,5,5,8,8)
v2=c(2,5,2,8,2,2)
v3=c(3,6,3,9,3,3)
v4=c(4,7,3,4,7,4)
data=data.frame(v1,v2,v3,v4)
head(data)
datav2,datav2v5)
tarid=names(a)[a>2]
res=data[data$v2v5%in%tarid,]
print(res)