Manipulation of Dataframe in R

Asked

Viewed 121 times

1

I have the dataset as per the figure. I need to name the Mes column where it is 1 is January, 2 February and so on. I still need to group by type of expense and add the reimbursed amount. The idea is to generate monthly graphs by name, type of expense and by the total amount reimbursed. I’m bumping heads here. Someone can help me. Thank you.

inserir a descrição da imagem aqui

  • Hello Edi, the quickest way to help you would be if you made available a reproducible sample of your data set, this posted image is even explanatory, but not reproducible, I suggest that in the next posts use the command dput(dataset), this will make it much easier to get an answer.

  • Thank you. I will follow the recommendation.

1 answer

1

Take as a hypothetical example this data set

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
  • Hello Fernandes. I will reproduce here and come back to inform the result. But it has already given a whitening. Thanks.

  • Hello Fernandes/Community. I followed your recommendation and it worked super well. Thank you. The only thing is that the sum result is returning the value as scientific notation: the code is: group_by(senado_acir, expense) %>% summarise(Total = sum(expense)) it groups the types of expenditure and sums up the expenses. Until then perfect, but the sum returns a scientific notation such as: 1.57e5 that would be 1.57*10 5 . It has how to transform this in R?

  • Use the command options(scipen = 15) at the beginning of your script, it will play values up to 15 digits.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.