r - Bin data into quantiles using cut - getting missing values -
i have dataframe:
a <- matrix(c(1,2,3,4), 2,2) colnames(a) <- c("a", "b") df <- as.data.frame(a) > df b 1 1 3 2 2 4
first, calculate quartilies of "a" column:
> quantile (df$a) 0% 25% 50% 75% 100% 1.00 1.25 1.50 1.75 2.00
then, categorize column "b" using quartiles column "a":
> cat.b<-cut(df$b, quantile (df$a,)) > cat.b [1] <na> <na> levels: (1,1.25] (1.25,1.5] (1.5,1.75] (1.75,2]
as can see, r gives na both "b" values above highest quartile of "a".
however, resulting "cat.b" vector like:
> cat.b [1] ">2" ">2"
could please tell me how in r.
thank you
the 2 values not in range. can circumvent making break. chose inf
, can use finite value.
cat.b <- cut(df$b, c(-inf, quantile(df$a), inf)) #next line can done better, illustrates purpose levels(cat.b)[length(levels(cat.b))] <- ">2" levels(cat.b)[1] <- "<1" cat.b [1] >2 >2 levels: (1,1.25] (1.25,1.5] (1.5,1.75] (1.75,2] >2
Comments
Post a Comment