-1

I'm new to R and I would like to know how to apply a calculation on a R table named "table" with two columns "number" & "ageRange". In this table I numbers (in the "number" column) which are associated with an age range ("ageRange") [0,9,19,29,39,49,59,69,79,89,90].

But there is multiple numbers for each age range, ex :

[number, ageRange ;
255, 0 ; 
351, 0 ; 
145, 9 ;
...]

What I wanted to do is to additionate every number with a same "ageRange". So I would like to know if there is a way in R to apply this comparative function on the entire table ?

Thanks for your help ^^.

Jpn287
  • 21
  • 6

1 Answers1

0

Have a look at the package dplyr which provides powerful data manipulation functions: https://dplyr.tidyverse.org/articles/dplyr.html

Here, you can use group_by and summarise:

data <- data.frame(
  number = c(255, 351, 145),
  ageRange = as.factor(c(0, 0, 9))
)

library(dplyr)
data %>% 
  group_by(ageRange) %>% 
  summarise(sum_number = sum(number))
# A tibble: 2 x 2
  ageRange sum_number
  <fct>         <dbl>
1 0               606
2 9               145
starja
  • 7,722
  • 1
  • 7
  • 22