问题
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