I am using R to calculate some basic statistic results. I am using the quantile() function,to calulate quantiles on a data frame column as follows.
> quantile(foobars[,1])
0% 25% 50% 75% 100%
189000 194975 219500 239950 1000000
I want to be able to individually access the calculated quantiles. However, I can’t seem to find out how to do that. When I check the class of the returned result, it a 1 dimensional numeric.
I tried this:
> q <- quantile(foobars[,1])
> q[3]
50%
219500
Which seems to return a tuple (quantile level + number). I am only interested in the number (219500 in this case.
How may I access only the number into a simple (numeric) variable?
You are confusing the printed representation of the numeric value with the actual value. As far as R is concerned,
qcontains a named numeric vector:All the “named” bit means is that the vector has an attached attribute
"names"containing the (in this case) quantile labels. R prints these for a named vector as they are considered helpful to have in printed output if present. But, they in no way alter the fact that this is a numeric vector. You can use these in computations as if they didn’t have the"names"attribute:If the names bother you, the
unname()function exists to remove them:For completeness, I should probably add that you can extract the
"names"using thenames()function, which also has an assignment version ('names<-'()). So another way to remove the names from a vector is to assignNULLto the names: