[R] Add a column to a data frame with value based on the percentile of the row

2013-07-31 Thread Dark
Hi all, I think this should be an easy question for the guru's out here. I have this large data frame (2.500.000 rows, 15 columns) and I want to add a column named "SEGMENT" to it. The first 5% rows (first 125.000 rows) should have the value "Top 5%" in the SEGMENT column Then the rows from 5% to

Re: [R] Add a column to a data frame with value based on the percentile of the row

2013-07-31 Thread Rui Barradas
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

Re: [R] Add a column to a data frame with value based on the percentile of the row

2013-07-31 Thread arun
MENT #495  1  0.3571848 Bottom 50 #496  9 -1.1971854 Bottom 50 #497  5  0.3544896 Bottom 50 #498  8 -0.1562356 Bottom 50 #499  8 -0.2994321 Bottom 50 #500  8 -0.4170319 Bottom 50 A.K. - Original Message - From: Dark To: r-help@r-project.org Cc: Sent: Wednesday, July 31, 2013 5:37 AM Subjec

Re: [R] Add a column to a data frame with value based on the percentile of the row

2013-07-31 Thread arun
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

Re: [R] Add a column to a data frame with value based on the percentile of the row

2013-07-31 Thread Rui Barradas
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

Re: [R] Add a column to a data frame with value based on the percentile of the row

2013-07-31 Thread Dark
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. __

Re: [R] Add a column to a data frame with value based on the percentile of the row

2013-07-31 Thread Dark
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(