r - Number of unique values within a range in data-frame -


From a data frame, I want to remove the number of unique values ​​(x) within a certain range of w (eg Up to 3000), each 0-100, 101-200, 201-300, etc.

Example DF

  XY 16 9 183 546 64 154 148 593 203 60 243 568 370 85 894 168 169154 148 83 897 ...  < / Pre> 

A time consuming method would be to run the following code for each category:

  junk < -subset (df, y & gt; 0 & amp; Y & lt; 100) Length (unique (junk $ record.no))  

But I have to ask experts - a better way?

You can get it from () and cut () :

  data & lt; - data.frame (x = roof (rnorm (10000, 500, 10)), y = ricf (10000, 0, 3000)) data $ group & lt; - Create a serial variable for each category (Data $ Y, Data $ group, Function (X) Length (Unique (x))  - Cut (Data $ Y, CEC (0, 3000, 100)) 

Comments