[R] binned column in a data.frame

Mike Meredith mmeredith at wcs.org
Fri Jul 20 11:46:12 CEST 2007



Try something like this:

x <- c(1,2,6,8,13,0,5,10, runif(10) * 100)
tmp <- x %/% 5 + 1
bin.names <- paste((0:19) * 5, (1:20) * 5, sep="-")
data.frame(Start = x, Binned_Start = bin.names[tmp])

This assumes you want (eg.) 5 in the 5-10 bin, not the 0-5 bin. You may also
want to make Binned_Start into a factor rather than a character vector.

HTH, Mike.



João Fadista wrote:
> 
> Dear all,
>  
> I would like to know how can I create a binned column in a data.frame. The
> output that I would like is something like this: 
>  
> Start  Binned_Start
> 1        0-5
> 2        0-5
> 6        5-10
> 8        5-10
> 13      10-15
> ...            
>  
>  
>  
> 
> Best regards
> 
> João Fadista
> Ph.d. student
> 
> 
>  	
>  	 UNIVERSITY OF AARHUS	
> Faculty of Agricultural Sciences	
> Dept. of Genetics and Biotechnology	
> Blichers Allé 20, P.O. BOX 50	
> DK-8830 Tjele	
>  	
> Phone:	 +45 8999 1900	
> Direct:	 +45 8999 1900	
> E-mail:	 Joao.Fadista at agrsci.dk <mailto:Joao.Fadista at agrsci.dk> 	
> Web:	 www.agrsci.org <http://www.agrsci.org/> 	
> ________________________________
> 
> News and news media <http://www.agrsci.org/navigation/nyheder_og_presse> .
> 
> This email may contain information that is confidential. Any use or
> publication of this email without written permission from Faculty of
> Agricultural Sciences is not allowed. If you are not the intended
> recipient, please notify Faculty of Agricultural Sciences immediately and
> delete this email.
> 
> 
> 	[[alternative HTML version deleted]]
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/binned-column-in-a-data.frame-tf4115787.html#a11705240
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list