DataFrame Handling in R

0

I have the dataset according to the figure. I need to name the Month column where where 1 is January, 2 February and so on. I also need to group by type of expense and add the amount reimbursed. The idea is to generate monthly charts by name, type of expense and by the total amount reimbursed. I'm pounding my head here. Can someone help me. Thank you.

    
asked by anonymous 10.09.2018 / 18:11

1 answer

0

Take this dataset as a hypothetical

Mes = c(1,2,2,4,3)
Nome = c("ACIR G","ACIR G","ACIR G","ACIR G","ACIR G")
Tipo = c("Aluguel", "Aluguel","Aluguel", "Passagem", "Passagem")
Valor = c(5, 10, 15, 20, 25)

(dataset = data.frame(Mes, Nome, Tipo, Valor))
#  Mes   Nome     Tipo Valor
#1   1 ACIR G  Aluguel     5
#2   2 ACIR G  Aluguel    10
#3   2 ACIR G  Aluguel    15
#4   4 ACIR G Passagem    20
#5   3 ACIR G Passagem    25

# tipo de dados
str(dataset)

# transformando a coluna em factor
(dataset$Mes = factor(dataset$Mes))
#[1] 1 2 2 4 3
#Levels: 1 2 3 4

# categorizando os levels da coluna 
levels(dataset$Mes) =  factor(c("Jan", "Fev", "Mar", "Abr"))
#[1] Jan Fev Mar Abr
#Levels: Abr Fev Jan Mar

library(dplyr)

# valor total por tipo 
group_by(dataset, Tipo) %>% summarise(Total = sum(Valor))
# A tibble: 2 x 2
#  Tipo     Total
#  <fct>    <dbl>
#1 Aluguel     30
#2 Passagem    45

# valor total por mês segundo o tipo 
group_by(dataset,  Mes, Tipo) %>% summarise(Total = sum(Valor))
# A tibble: 4 x 3
# Groups:   Mes [?]
#  Mes   Tipo     Total
#  <fct> <fct>    <dbl>
#1 Jan   Aluguel      5
#2 Fev   Aluguel     25
#3 Mar   Passagem    25
#4 Abr   Passagem    20
    
10.09.2018 / 19:34