-1

I have numbers starting from 1 to 6000 and I want it to be separated in the manner listed below.

1-10 as "Range1" 10-20 as "Range2" 20-30 as ""Range3" . . . 5900-6000 as "Range 600".

I want to calculate the range with equal time interval as 10 and at last I want to calculate the frequency as which range is repeated the most.

How can we solve this in R programming.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
karthik_19942017
  • 49
  • 1
  • 2
  • 6
  • 1
    Do you mean 5990-6000? – Elin Jun 18 '17 at 06:56
  • Please provide an [input example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and the expected output. – digEmAll Jun 18 '17 at 07:08
  • See `?cut` and `?seq` – Richard Telford Jun 18 '17 at 09:31
  • 1
    Input example would be consider a set of students and assume there grades such as A,B,C,D and the equivalent Marks for their corresponding grades would be 100,90,80,70 and I need to specify that if the marks IS between 90-100 specify it as "grade a" and atlast I need to find the frequency as which grade is being repeated the most – karthik_19942017 Jun 19 '17 at 08:29

2 Answers2

0

You should use the cut function and then table can determine the counts in each category and sort in order of the most prevalent.

x <- 1:6000
x2 <- cut(x, breaks=seq(1,6000,by=10), labels=paste0('Range', 1:599))
sort(table(x2), descending = TRUE)
Steven M. Mortimer
  • 1,618
  • 14
  • 36
0

There is a maths trick to you question. If you want categories of length 10, round(x/10) will create a category in which 0-5 will become 0, 6 to 14 will become 1, 15 to 24 will become 2 etc. If you want to create cat 1-10, 11-20, etc., you can use round((x+4.1)/10).

(i don't know why in R round(0.5)=0 but round(1.5)=2, that's why i have to use 4.1)

Not the most elegant code but maybe the easiest to understand, here is an example:

# Create randomly 50 numbers between 1 and 60
x = sample(1:60, 50)

# Regroup in a data.frame and had a column count containing the value one for each row
df <- data.frame(x, count=1)
df

# create a new column with the category
df$cat <- round((df$x+4.1)/10)

# If you want it as text:
df$cat2 <- paste("Range",round((df$x+4.1)/10), sep="")
str(df)

# Calculate the number of values in each category
freq <- aggregate(count~cat2, data=df, FUN=sum)

# Get the maximum number of values in the most frequent category(ies)
max(freq$count)

# Get the category(ies) name(s)
freq[freq$count == max(freq$count), "cat2"]
David
  • 100
  • 1
  • 8