[R] bin a vector of continuous variables

Kehl Dániel kehld at ktk.pte.hu
Thu May 15 12:11:39 CEST 2014


Hi,

you might try writing your own function, something like

roundDown <- function(x) floor(x/100)*100

> roundDown(c(2282,174,273,432))
[1] 2200  100  200  400

and than apply it to your column.

Please use ?dput next time to present your data, and post in plain text, instead of HTML.

best,
daniel
________________________________________
Feladó: r-help-bounces at r-project.org [r-help-bounces at r-project.org] ; meghatalmazó: Assa Yeroslaviz [frymor at gmail.com]
Küldve: 2014. május 15. 11:54
To: R help forum
Tárgy: [R] bin a vector of continuous variables

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