# Sum various factor together in a column

Hello everybody,

I would like to know if you can help me, I've got actually a large dataframe with too much details about one factor ('oak') and I would like to sum each type (1,2,3) into one summed (reducing the number of rows by the way)

Imagine a table like the one below with a number of indexes of a farmer. This index is present several times because the same farmer may grow several different products (oak 1 oak2 & rose for example).

My question is how to sum up the oak1 oak2 oak3 rows for each farmer and for each variable in the table.
My dataframe looks like something like that :

``````Farmer_index   <- c("50" ,"50", "15", "89", "20", "20", "20", "4", "4", "38", "72", "77", "18",
"94", "47", "100", "48", "48", "98" , "60", "45", "36", "29")

Plant          <- c("Oak1", "Oak2", "Rose", "Carott", "Oak3","Oak1",
"Rose", "Rose", "Rose", "Carott", "Carott","Oak1",
"Oak1", "Oak2", "Rose", "Carott", "Oak3","Oak1",
"Oak2","Carott","Oak3","Carott","Rose"
)

Surface_ha     <- c(13, 1000 , 12 , 5 , 85 , 78 , 0,
99 , 44 , 45 , 70 , 1000 , 9 , 1,21,
100, 101 , 10, 20, 30, 40, 5, 99)

Whatever      <- c(130, 100 , 12 , 5 , 85 , 78 , 0,
99 , 44 , 45 ,36 , 58 , 9 , 1,21,
100, 101 , 10, 20, 30, 40, 5, 9)

df <- data.frame(Farmer_index,Plant, Surface_ha,Whatever)

print (df)
``````

Hope you can help me with it ! It would be really useful for me

``````library(tidyverse)

(result_df <- df |> mutate(grp=if_else(str_detect(Plant,"Oak"),
"Oak",Plant)) |>
group_by(Farmer_index,grp) |>
summarise(across(where(is.numeric),sum)))``````
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.