Removing repeated obs data if n Obs < x in R

228 views Asked by At

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,]
2

There are 2 answers

11
Joris Meys On

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.

3
Manuel Ramón On
x[x %in% names(table(x)[table(x) >=3])]