Ronald Bielester
Ronald Bielester

Reputation: 45

How to take frequencies of different ranges using R?

I have a set of data which refers to Velocities of different people...so my data looks like this So its basically just a list of velocities

Velocity(m/s)
1.2
1.4
2.3
1.6
4.5
3.5
1.7
4.5
3.7

I want to use R to get different ranges and the frequency of that particular range... e.g I want an output similar to the one below

Velocity(m/s)     Frequency
1.0 - 3.0            7
1.5- 2.0             4

...

So I want to get a frequency column for various ranges. I'm really new to R and would really like to use it to analyse some of my data.

Upvotes: 1

Views: 4700

Answers (2)

flodel
flodel

Reputation: 89057

I would create a data.frame with minimum and maximum velocities for each range:

my.summary <- data.frame(vel.lo = c(1, 1.5), 
                         vel.hi = c(3, 2))

Then use plyr's adply for counting the frequencies:

library(plyr)
adply(my.summary, 1, transform, freq = sum(dat$Velocity  > vel.lo &
                                           dat$Velocity <= vel.hi))
#   vel.lo vel.hi freq
# 1    1.0      3    5
# 2    1.5      2    2

Note: you can also just use the base package's apply but since it drops column names, I find adply gives you code that reads better than:

my.summary$freq <- apply(my.summary, 1, function(x)sum(dat$Velocity  > x[1] &
                                                       dat$Velocity <= x[2]))

Upvotes: 1

csgillespie
csgillespie

Reputation: 60452

You can do this easily with a combination of cut and table. First lets generate 100 random numbers between 0 & 100:

r = runif(100, 0, 100)

Next we where we want the breaks to appear. In this example, we want them at: 0, 10, ..., 100:

cut(r, breaks=seq(0, 100, 10))

Then use the table command to calculate the frequency:

table(cut(r, breaks=seq(0, 100, 10)))

You seem to want overlapping breaks, so I would use the table command twice, i.e.

t1 = table(cut(r, breaks=seq(0, 100, 10)))
t2 = table(cut(r, breaks=seq(0, 100, 5)))

Then combine the results

c(t1, t2)

Upvotes: 4

Related Questions