dataframe - R: Using which() with output of quantile()? -
i'm new r, sorry if weird question.
so, i'm trying value in column data frame df, 90th percentile of column b. used following code value @ quantile:
p = quantile(df$b, c(0.9)) after this, wanted use row number of value, use corresponding value in column a:
which(df$b == p) but reason gives output of
integer(0) i substituted variable, p, actual value, 1.68, , worked, , creating variable 1.68 works too, using result of quantile never gives right value.
i've tried using as.numeric, using p[[1]] , as.double. no change result whatsoever. appreciated in understanding why happens , if there's way go this.
edit: clarify, there entries @ 90th percentile, , using hard coded value returned quantile function return these. problem occurs in function when use output of quantile.
'quantile' give the 90% percentile of data may or may not value in data therefore if want value closest 90% percentile need match little differently:
your data:
df <- data.frame(b = runif(50)) the 90th percentile
p = quantile(df$b, c(0.9)) the index closest 90th percentile:
index <- which(abs(df$b - p) == (min(abs(df$b - p), na.rm = true))) index get value(s) dataframe using index:
df$b[index]
Comments
Post a Comment