I have a data.frame DT_new
with 4 columns :
Sample:
Graduated Work Married Jumlah
2015-05-01 2015-05-02 2015-05-03 20
NA 2015-05-02 2015-05-03 20
NA NA 2015-05-03 20
NA 2015-05-02 NA 20
I need to aggregate Jumlah
by date in Graduated
or Work
or Married
Graduated
value is not NA
, use date from Graduated
Graduated
value is NA
, use another value from Work
or
Married
format what I want is :
Dates Total
2015-05-01 10
2015-05-02 40
2015-05-03 30
I have tried aggregate
with group by in R but just group by 1 column (Graduated), such as:
DT_Totals = DT_Total %>%
group_by(Graduated) %>%
summarise(Total= sum(Jumlah)) %>%
arrange(Graduated)
How can I handle my problem?
Here we need to specify an if…then… else statement. To do so within the mutate() function we use the function called ifelse() . ifelse() evaluates a logical statement specified in the first argument, RT < 200 .
In R programming, the mutate function is used to create a new variable from a data set. In order to use the function, we need to install the dplyr package, which is an add-on to R that includes a host of cool functions for selecting, filtering, grouping, and arranging data.
To use mutate in R, all you need to do is call the function, specify the dataframe, and specify the name-value pair for the new variable you want to create.
mutate() adds new variables and preserves existing ones; transmute() adds new variables and drops existing ones.
You need first create new column and then group over them.
I got function to return first not NA element in vectors defined as:
first_not_na <- function(...) {
Reduce(list(...), f=function(x,y) {
x[is.na(x)] <- y[is.na(x)]
x
})
}
And you can use it as follow
DT_new %>%
group_by(Date = first_not_na(Graduated, Work, Married)) %>%
summarise(Total = sum(Jumlah)) %>%
arrange(Date)
or splitting to two steps:
DT_new %>%
mutate(Date = first_not_na(Graduated, Work, Married)) %>%
group_by(Date) %>%
summarise(Total = sum(Jumlah)) %>%
arrange(Date)
Just create a new date column using ifelse
:
DT_new %>%
mutate(Date1 = ifelse(!is.na(Graduated), Graduated, ifelse(!is.na(Work), Work, Married))) %>%
group_by(Date1) %>%
summarise(Total = sum(Jumlah)) %>%
arrange(Date1)
In case the dates are numeric (Date) type:
DT_new %>%
mutate(Date1 = ifelse(!is.na(Graduated), Graduated, ifelse(!is.na(Work), Work, Married))) %>%
mutate(Date1 = as.Date(Date1, origin = "1970-01-01")) %>%
group_by(Date1) %>%
summarise(Total = sum(Jumlah)) %>%
arrange(Date1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With