SUMMATION IN A COLUMN IN A LINE INTERVAL

Asked

Viewed 64 times

-1

inserir a descrição da imagem aqui I have the following database: I would like to add the INTE_C1 for each month.

That is, for January add from 1/1/2000 to 1/31/2000...

  • Welcome to Stackoverflow! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please, take a look at this link and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

2 answers

1

You can also use the package dplyr

library(dplyr)
summary <-  dados %>%
                  mutate (Date = as.Date(Date, "%m/%d/%Y"),
                          Mes = as.Date(Date, "%Y%m")) %>%
                 group_by (Mes) %>%
                 summarise (Soma = sum (INTE_C1, na.rm=TRUE)) %>%
                 ungroup ()

If you wanted to apply this to all columns you can do as follows

summary <-  dados %>%
                  mutate (Date = as.Date(Date, "%m/%d/%Y"),
                          Mes = as.Date(Date, "%Y%m")) %>%
                 group_by (Mes) %>%
                 summarise_at (vars(INTE_C1:SO2), sum, na.rm = TRUE) %>%
                 ungroup ()

If you foresee more functions you can use the following:

summary <-  dados %>%
                  mutate (Date = as.Date(Date, "%m/%d/%Y"),
                          Mes = as.Date(Date, "%Y%m")) %>%
                 group_by (Mes) %>%
                 summarise_at (vars(INTE_C1:SO2), list (sum, mean, sd), na.rm = TRUE) %>%
                 ungroup ()

Another suggestion is to use working the data in the long format as follows:

library (tidyr)
summary <-  dados %>%
                  mutate (Date = as.Date(Date, "%m/%d/%Y"),
                          Mes = as.Date(Date, "%Y%m")) %>%
                 gather (key = "Parametro", value = "Resultado", INTE_C1:SO2) %>% 
                 group_by (Mes, Parametro) %>%
                 summarise (Soma =  sum(Resultado, na.rm = TRUE),
                            Media =  mean(Resultado, na.rm = TRUE)) %>%
                 ungroup ()
  • in long format it is easier to work the data, create loops etc

0

Here are two ways to add one column per month.

First you have to turn the column Date at a true date.

dados$Date <- as.Date(dados$Date, "%m/%d/%Y")

Now, add the column INTE_C1 with aggregate.

Only with R base, the format creates a class vector "character" with year and month.

mes <- format(dados$Date, "%Y-%m")
aggregate(INTE_C1 ~ mes, dados, sum)

With the package zoo, a vector with year and month of class is also obtained "yearmon" (year Month).

mes <- zoo::as.yearmon(dados$Date)
aggregate(INTE_C1 ~ mes, dados, sum)

Code to create test data.

set.seed(1234)

Date <- seq(as.Date("2000-01-01"), Sys.Date(), by = "day")
Date <- format(Date, "%m/%d/%Y")
INTE_C1 <- sample(20, length(Date), TRUE)
dados <- data.frame(Date, INTE_C1)

Browser other questions tagged

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