Jonathan Beard
2010-May-28 19:00 UTC
[R] create new variable: percentile value of variable in data frame
Hello all, Thanks in advance for you attention. I would like to generate a third value that represents the quantile value of a variable in a data frame. # generating data x <- as.matrix(seq(1:30)) y <- as.matrix(rnorm(30, 20, 7)) tmp1 <- cbind(x,y) dat <- as.data.frame(tmp1) colnames(dat) <- c("id", "score") dat # finding percentiles of "score" qs <- as.matrix(quantile(dat$score, type=3, probs = seq(0,1,.1))) colnames(qs) <- c( "score") qs # is there a way to put the quantile value for a value of 'score' into a new variable, # such that the new data frame would have three variables: id, score and q.score? ## running R version 2.8.1 (2008-12-22) on Vista Thanks so much! -Jon
Stephan Kolassa
2010-May-28 20:06 UTC
[R] create new variable: percentile value of variable in data frame
Hi Jon, does the empirical cumulative distribution function do what you want? dat$q.score <- ecdf(dat$score)(dat$score) ?ecdf HTH Stephan Jonathan Beard schrieb:> Hello all, > > Thanks in advance for you attention. > I would like to generate a third value that represents the quantile > value of a variable in a data frame. > > > # generating data > > x <- as.matrix(seq(1:30)) > y <- as.matrix(rnorm(30, 20, 7)) > tmp1 <- cbind(x,y) > dat <- as.data.frame(tmp1) > colnames(dat) <- c("id", "score") > dat > > # finding percentiles of "score" > > qs <- as.matrix(quantile(dat$score, type=3, probs = seq(0,1,.1))) > colnames(qs) <- c( "score") > qs > > # is there a way to put the quantile value for a value of 'score' > into a new variable, > # such that the new data frame would have three variables: id, score > and q.score? > > ## running R version 2.8.1 (2008-12-22) on Vista > > > Thanks so much! > > -Jon > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Apparently Analagous Threads
- Getting empirical percentiles for data
- data vector to corresonding percentile ranks
- Percentile rank for each element in list
- Why 95% "quantile" empty in R or why 95% "quantile" = 1 with data values between 0 and 1?
- percentile of a given value: is there a "reverse" quantile function?