I have a large dataset of over 10,000 rows: df:
User duration
amy 582
amy 27
amy 592
amy 16
amy 250
tom 33
tom 10
tom 40
tom 100
Desired output:
User duration
amy 582
amy 592
amy 250
tom 33
tom 10
tom 40
Essentially, this will be removing any outliers that are 2SD from each unique User mean. The code would take the mean of each unique user, determine its mean and standard deviation and then remove values that are > 2SD of the mean.
dput:
structure(list(User = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L), .Label = c("amy", "tom"), class = "factor"), duration = c(582L,
27L, 592L, 16L, 250L, 33L, 10L, 40L, 100L)), class = "data.frame", row.names = c(NA,
-9L))
This is what I have tried:
first define average and standard deviation
ave = ave(df$duration)
sd = sd(df$duration)
And then set some sort of parameter to this:
for i in df {
remove all if > 2*sd}
I am not sure and would like some suggestions.