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