Reputation: 13
This might be very basic in R, but I am trynig to think of a way how to do this and have no idea how to google it.
I have this:
Age Count
1 5
2 1
3 2
4 1
5 1
and I want this:
Age Count
1-5 10
Upvotes: 1
Views: 52
Reputation: 13319
Using dplyr
library(dplyr)
df %>%
mutate(Int=findInterval(Age,c(1,5),rightmost.closed = T),
Age=ifelse(Int==1,"1-5",Age)) %>%
group_by(Age) %>%
summarise(Count=sum(Count))
# A tibble: 1 x 2
Age Count
<chr> <int>
1 1-5 10
Upvotes: 0
Reputation: 8364
You can do this by using the cut
function.
This will put age
in intervals specified via the breaks
argument.
d$int <- cut(d$Age, breaks = c(-Inf, 5), labels = "1-5") # create your new interval
#d
# Age Count int
# 1 1 5 1-5
# 2 2 1 1-5
# 3 3 2 1-5
# 4 4 1 1-5
# 5 5 1 1-5
Use aggregate
to sum
by the new int
column
aggregate(Count ~ int, data=d, sum)
# int Count
# 1 1-5 10
Data:
d <- read.table(text = "Age Count
1 5
2 1
3 2
4 1
5 1", header=T)
Upvotes: 2