How to edit values in a column?

Asked

Viewed 30 times

1

Hello, I have a dataset with the following columns

date          total_cases
2019-12-31       0      
2020-01-01       0      
2020-01-02       0      
2020-01-03       0      
2020-01-04       0      
2020-01-05       0  

I am trying to take days and leave only the year and month, and then I will unite them using the Aggregate function. can give me a hint of some way for me to do this procedure?

Note: I imported the data as follows

dados = read.csv(file = "raw_data.csv", sep = ",", na.strings = "", strip.white = T)

1 answer

1


Maybe this will help you

library(lubridate)

df$dia <- day(df$date)
df$ano_mes <- paste0(year(date),'-',month(date))

Using the lubridate package we were able to extract the day, month and year. In this case I created new columns in the data frame but you can do otherwise if you prefer.

Exit

        date total_cases dia ano_mes
1 2019-12-31           0  31 2019-12
2 2020-01-01           0   1  2020-1
3 2020-01-02           0   2  2020-1
4 2020-01-03           0   3  2020-1
5 2020-01-04           0   4  2020-1
6 2020-01-05           0   5  2020-1

You can also extract one by one and put in new columns

df2$dia <- day(df2$date)
df2$mes <- month(df2$date)
df2$ano <- year(df2$date)

Exit

        date total_cases dia mes  ano
1 2019-12-31           0  31  12 2019
2 2020-01-01           0   1   1 2020
3 2020-01-02           0   2   1 2020
4 2020-01-03           0   3   1 2020
5 2020-01-04           0   4   1 2020
6 2020-01-05           0   5   1 2020

Browser other questions tagged

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