[R] bin a vector of continuous variables

arun smartpink111 at yahoo.com
Thu May 15 12:05:09 CEST 2014



Hi,
check ?cut or ?findInterval
dat <- read.table(text="partner1 partner2 Substract
1_5000_5001_10000        1    5001      2282.3435
1_5000_10001_15000        1    10001      174.1275
1_5000_15001_20000        1    15001      273.822
1_5000_20001_25000        1    20001      546.27
1_5000_25001_30000        1    25001      701.299
1_5000_30001_35000        1    30001      189.2345",sep="",header=TRUE)

 dat <- within(dat, binnedData <- (findInterval(Substract, seq(0,2300,by=100))-1)*100)
 dat
#                   partner1 partner2 Substract binnedData
#1_5000_5001_10000         1     5001 2282.3435       2200
#1_5000_10001_15000        1    10001  174.1275        100
#1_5000_15001_20000        1    15001  273.8220        200
#1_5000_20001_25000        1    20001  546.2700        500
#1_5000_25001_30000        1    25001  701.2990        700
#1_5000_30001_35000        1    30001  189.2345        100

A.K.


On Thursday, May 15, 2014 5:57 AM, Assa Yeroslaviz <frymor at gmail.com> wrote:
Hi all,

I have a data.frame of three columns:
                   partner1 partner2 Substract
1_5000_5001_10000         1     5001      2282.3435
1_5000_10001_15000        1    10001       174.1275
1_5000_15001_20000        1    15001       273.822
1_5000_20001_25000        1    20001       546.27
1_5000_25001_30000        1    25001       701.299
1_5000_30001_35000        1    30001       189.2345
...

I would like to create a new column for this data, but to bin the variable
into separate bin of 0-100, 100-200, 200-300, 300-400, etc.
so that the new data will look like that
                   partner1 partner2 Substract    binnedData
1_5000_5001_10000         1     5001      2282.3435    2200
1_5000_10001_15000        1    10001       174.1275    100
1_5000_15001_20000        1    15001       273.822     200
1_5000_20001_25000        1    20001       546.27      500
1_5000_25001_30000        1    25001       701.299     700
1_5000_30001_35000        1    30001       189.2345    100
...

Can someone give me a hint how to achieve this conversion?

thanks
Assa

    [[alternative HTML version deleted]]

______________________________________________
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.



More information about the R-help mailing list