Removing repeated obs data if n Obs < x in R

为君一笑 提交于 2019-12-24 05:48:46

问题


I have a repeated measures data set. I need to remove all Participants where the number of observations for that individual is less than 3. What is the best way to do this?

x <- c(9, 9, 9, 11, 11, 23, 23, 23, 23, 45, 45, 45, 56, 56)

Here 11 and 56 need to be removed from the data. So far I have created a data frame with all the obs that I want to keep but not sure how to manipulate my data set using the new data frame

x <- as.data.frame(table(x))
x1 <- x[x$Freq > 2,]

回答1:


x[x %in% names(table(x)[table(x) >=3])]



回答2:


One more for the ave() function :

x[ave(x,x,FUN=length) > 2]

In an answer to your comment, you should perform it like this :

raw.data1 <- raw.data[ave(raw.data$REGISTRA,raw.data$REGISTRA,FUN=length) > 2]

Also read the help page of ave, that will help you understand what the code is doing exactly.



来源:https://stackoverflow.com/questions/8036700/removing-repeated-obs-data-if-n-obs-x-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!