How to generate bin frequency table in R?

前端 未结 4 1940
暖寄归人
暖寄归人 2020-12-15 10:04

How can i bin data of size 0.1 for the following example.

x<-c(0.01,0.34,0.45,0.67,0.89,0.12,0.34,0.45,0.23,0.45,0.34,0.32,0.45,0.21,0.55,0.66,0.99,0.23,.         


        
4条回答
  •  無奈伤痛
    2020-12-15 10:50

    Akrun's answer was good but didn't quite get me there for formatting.

    x<-c(0.01,0.34,0.45,0.67,0.89,0.12,0.34,0.45,0.23,0.45,0.34,0.32,0.45,0.21,0.55,0.66,0.99,0.23,.012,0.34)
    
    cuts<-cut(x, breaks=seq(0,1, by=0.1))
    counts<-c(t(table(cuts)))
    
    #Here's the important part for me, formatting the cuts for display in the data frame:
    
    labs <- levels(cuts)
    lable_matrix<-cbind(lower = as.numeric( sub("\\((.+),.*", "\\1", labs) ),
      upper = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", labs) ))
    
    cut_frame<-data.frame(lable_matrix,counts)
    
    #   lower upper counts  
    #1    0.0   0.1      2  
    #2    0.1   0.2      1  
    #3    0.2   0.3      3  
    #4    0.3   0.4      5  
    #5    0.4   0.5      4  
    #6    0.5   0.6      1  
    #7    0.6   0.7      2  
    #8    0.7   0.8      0  
    #9    0.8   0.9      1  
    #10   0.9   1.0      1  
    

提交回复
热议问题