Last active
October 10, 2019 20:35
-
-
Save Verginio/d671ff36b852c68b6ba7b7f59114a22e to your computer and use it in GitHub Desktop.
exemplo_1.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################# | |
## PASSO 1: Carregue os pacotes necessários no início do código | |
################################################################# | |
library(dplyr) | |
################################################################# | |
## PASSO 2: Geração de dados: | |
################################################################# | |
## - Opção 1: gere dados aleatórios fixando a semente: | |
################################################################# | |
set.seed(1) | |
##Cria data.frame de CPF, Nomes e Descrição | |
df_aluno <- data.frame(CPF = 1:26, | |
Nome = LETTERS, | |
Descrição = letters) | |
##Define o número de linhas | |
n_linhas <- 1000 | |
##Gera um vetor aleatório entre 1 e 26 sem casa após a vírgula | |
k <- runif(n = n_linhas, min = 1, max = 26) %>% | |
round() | |
##organiza tudo em um data.frame para fins de teste | |
df_bolsa <- data.frame( | |
CPF = df_aluno$CPF[k], | |
Nome = df_aluno$Nome[k], | |
Descrição = df_aluno$Descrição[k], | |
Data = Sys.Date(), | |
Valor = runif(n_linhas, min = 200, max = 400) | |
) | |
##Verifica se esta de acordo com o exemplo dado: | |
head(df_bolsa) | |
################################################################# | |
## PASSO 3: Agrupa o data.frame por CPF e Nome com group_by | |
################################################################# | |
## e calcula as variáveis "Quantidade Parcelas", "Valor Mês" e "Valor Ano". | |
################################################################# | |
df_summ <- df_bolsa %>% | |
group_by(CPF, Nome) %>% | |
summarise( | |
`Quantidade Parcelas` = n(), | |
`Valor Mês` = mean(Valor), #média | |
`Valor Ano` = sum(Valor) #somatório | |
) | |
#Resultado | |
head(df_summ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment