A way to solve this problem using the R
basic is through the function apply
. using the data set provided by Tomás, we have the following:
txt <- "USUARIO jan fev mar abr mai jun jul ago set out nov dez
1160 0 1 1 1 1 1 1 1 1 1 1 1
2505 1 1 1 1 1 0 1 0 1 1 1 0
3042 1 1 1 0 0 0 1 1 1 1 1 0
3554 1 1 0 1 1 1 1 0 1 1 1 0 "
dados <- read.table(text = txt, header = TRUE)
The function apply
has three arguments:
The data set that we will analyze in array format (can be data frame or matrix, for example)
A value equal to 1 or 2. 1 indicates that we will apply a function in the array rows, while 2 indicates that we will apply a function in its columns
The function we will apply.
For your trouble, apply
may be applied as follows:
apply(dados, 2, sum)[-1]
# jan fev mar abr mai jun jul ago set out nov dez
# 3 4 3 3 3 2 4 2 4 4 4 1
I used the data frame dados
, with the function sum
applied in your columns (2
). As it has a user column, it is possible to remove it at the end using [-1]
, that informs to the R
remove the first position of the vector resulting from the application of apply
.
Suggestion: Before the first pipe
lvls <- format(seq(as.Date("2018-01-01"), as.Date("2018-12-01"), by = "month"), format = "%b")
and finish thegather
with%>% mutate(mes = factor(mes, levels = lvls))
to have the result ordered by calendar month and not by alphabetical order of months.– Rui Barradas