Hi Arun Kirshna,
I have tested your method and it will work for me.
I only run into one problem. Before I want to do this operation I have
sorted my data frame so my rownumbers ar not subsequent.
You can see if you first order your example data frame like:
dat1 <- dat1[order(-dat1$value),]
head(
Works like a charm, thanks a lot!
--
View this message in context:
http://r.789695.n4.nabble.com/Add-a-column-to-a-data-frame-with-value-based-on-the-percentile-of-the-row-tp4672711p4672728.html
Sent from the R help mailing list archive at Nabble.com.
__
Hello,
Sorry, that should be 0.80, not 0.70.
qq <- quantile(x, probs = c(0, 0.50, 0.80, 0.95, 1))
Rui Barradas
Em 31-07-2013 12:22, Rui Barradas escreveu:
Hello,
Combine quantile() with findInterval(). Something like the following.
# sample data
x <- rnorm(100)
val <- c("Bottom 50", "20
119 119 2.589574 20 to 50
12 12 2.512276 Top 5%
Do you have a solution for this?
- Original Message -
From: arun
To: Dark
Cc: R help
Sent: Wednesday, July 31, 2013 7:48 AM
Subject: Re: [R] Add a column to a data frame with value based on the
percentile of the row
Hi,
May
Hi,
May be this helps:
set.seed(24)
dat1<- data.frame(ID=1:500,value=rnorm(500))
indx<-round(quantile(as.numeric(row.names(dat1)),probs=c(0.05,0.20,0.50,1)))
indx1<-findInterval(row.names(dat1),indx,rightmost.closed=TRUE)
dat1$SEGMENT<- as.character(factor(indx1,labels=c("Top 5%","5 to 20","20 to
Hello,
Combine quantile() with findInterval(). Something like the following.
# sample data
x <- rnorm(100)
val <- c("Bottom 50", "20 to 50", "5 to 20", "Top 5%")
qq <- quantile(x, probs = c(0, 0.50, 0.70, 0.95, 1))
idx <- findInterval(x, qq)
val[idx]
Hope this helps,
Rui Barradas
Em 31-07
6 matches
Mail list logo