简体   繁体   中英

R Can't Find Quantile Value Within My Data?

I have a dataframe of several vectors of velocity data that I have smoothed with rollmean(). From my rolled means, I'd like to calculate the 0.75 and 1 quantiles with quantile(), and then find the row position of the first instance of those particular values. This is the code that I have written to do that:

df_quantilesummary <- df %>%
  group_by(TrialID) %>%
  summarize(MaxVel = max(RollingMean, na.rm=TRUE), Qant75Vel = quantile(RollingMean, probs=c(0.75), na.rm=TRUE),
            TotalLength = length(RollingMean),RowNumMax = which(grepl(max(RollingMean, na.rm=TRUE), RollingMean)),
            RowNum75 = which(grepl(quantile(RollingMean, probs=c(0.75), na.rm=TRUE), RollingMean))[1])

No errors are getting thrown and everything seems to be working well, however in my dataframe, quantile will calculate the 75th quantile perfectly fine, but I am finding which(grepl(...)) will sometimes return NA? As if the 75th quantile point it calculated doesn't exist. It won't be for every trial, just some of them. And the which(grepl(...)) works fine for the maximum value (it doesn't matter if I use max() or if I use quantile(x, probs=1), both ways work).

I made a mock dataframe below, but the code is working for it, so I am at a loss as to what is going on. Any insights would be helpful. Thank you.

set.seed(82828)
dummyvelocity1 <- c(runif(200, min=-0.0523, max=1))
set.seed(3983289389)
dummyvelocity2 <- c(runif(200, min=-0.1, max=1.2))
set.seed(227272)
dummyvelocity3 <- c(runif(200, min=-0.08, max=0.9))
set.seed(27272728393)
dummyvelocity4 <- c(runif(200, min=-0.02, max=1.45))
set.seed(1488)
dummyvelocity5 <- c(runif(200, min=-0.07, max=1.03))
Velocity <- c(dummyvelocity1, dummyvelocity2, dummyvelocity3, dummyvelocity4, dummyvelocity5)
TrialID <- c(rep(1, 200), rep(2, 200), rep(3, 200), rep(4,200), rep(5, 200)) 
df <- data.frame(TrialID, Velocity)
dflist <- split(df$Velocity, df$TrialID)
RollingMeanList <- lapply(dflist, function(x) rollmean(x, 20, fill=NA))
RollingMean <- unlist(RollingMeanList)
df <- cbind(df, RollingMean)

This is likely a precision issue.. You can try to use near()

df_quantilesummary <- df %>%
  group_by(TrialID) %>%
  summarize(
    MaxVel = max(RollingMean, na.rm=TRUE),
    Qant75Vel = quantile(RollingMean, probs=c(0.75), na.rm=TRUE),
    TotalLength = length(RollingMean),
    RowNumMax = which(near(RollingMean, max(RollingMean, na.rm=T))),
    RowNum75 = which(near(RollingMean, quantile(RollingMean, probs=0.75, na.rm=T)))
  )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM