Hi,
i need to clean some data. I believe I will need to pivot_long and mutate in order to get the sum of the values.
Any idea how could I do this?
Thanks in advance
#start
structure(list(...1 = c("Mercedes", "Opel", "VW"), T1 = c(1,
1, 2), T2 = c(3, 5, 2), T3 = c(5, 5, 2), T4 = c("1", "na", "2"
), year = c(2021, 2021, 2021)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L))```{r}
#what i would like to have
structure(list(...1 = c("Mercedes", "Opel", "VW", "T1", "T2",
"T3", "T4"), year = c(2021, 2021, 2021, NA, NA, NA, NA), total_T = c(NA,
NA, NA, 4, 10, 12, 3)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -7L))
I tried this but it did not work..
start |>
mutate(T1_Tot,sum(start$T1) )
pivot_longer(T1, names_to = "Tot1", values_to = "T_Total1") |>
pivot_longer(T2, names_to = "Tot2", values_to = "T_Total2") |>
pivot_longer(T3, names_to = "Tot3", values_to = "T_Total3") |>
pivot_longer(T4, names_to = "Tot4", values_to = "T_Total4") |>
mutate(start$total, sum(dat$T1))
start |>
mutate(Total_T =
case_when(T1= sum(T1) ~ "value") |>
case_when(T2 = sum (T2) ~ "value" ) |>
case_when(T3 = sum (T3) ~ "value" ) |>
case_when(T4 = sum (T4) ~ "value" ))