Merging and creating new column

With a dataframe like this:

structure(list(Mun = c("Argan", "Balb", "Barj", "Bemb", "Benz"
), PS_col2 = c(342L, 27L, 10L, 235L, 0L), PS_col3 = c(6010L, 
1250L, 0L, 475L, 0L), PS_col4 = c(5L, 1L, 3L, 0L, 0L), PP_col2 = c(87L, 
180L, 15L, 771L, 11L), PP_col3 = c(1528L, 8333L, 0L, 1560L, 258L
), PP_col4 = c(1L, 6L, 2L, 2L, 0L), CBR_col2 = c(135L, 0L, 0L, 
59L, 231L), CBR_col3 = c(2372L, 0L, 0L, 119L, 5422L), CBR_col4 = c(1L, 
0L, 0L, 0L, 4L), VO_col2 = c(0L, 0L, 0L, 113L, 184L), VO_col3 = c(0L, 
0L, 0L, 228L, 4319L), VO_col4 = c(0L, 0L, 0L, 0L, 3L), XBEMB_col2 = c(0L, 
0L, 0L, 2292L, 0L), XBEMB_col3 = c(0L, 0L, 0L, 4637L, 0L), XBEMB_col4 = c(0L, 
0L, 0L, 7L, 0L), BC_col2 = c(0L, 0L, 0L, 1107L, 0L), BC_col3 = c(0L, 
0L, 0L, 2239L, 0L), BC_col4 = c(0L, 0L, 0L, 3L, 0L), UP_col2 = c(0L, 
0L, 0L, 336L, 0L), UP_col3 = c(0L, 0L, 0L, 679L, 0L), UP_col4 = c(0L, 
0L, 0L, 1L, 0L)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

It's possible to merge two or more columns (like XBEMB_col2, BC_col2, UP_col2) in a new column with a new name? And same with other columns /XBEMB_col3, BC_col3, UP_col3/ also with new column and new name.

Could you provide a small example of what you want the new data set to look like? Thanks

Sure jrkrideau, here is the dataframe that I want to obtain:

df <- data.frame(
  stringsAsFactors = FALSE,
               Mun = c("Argan", "Balb", "Barj", "Bemb", "Benz"),
           PS_col2 = c(342L, 27L, 10L, 235L, 0L),
           PS_col3 = c(6010L, 1250L, 0L, 475L, 0L),
           PS_col4 = c(5L, 1L, 3L, 0L, 0L),
           PP_col2 = c(87L, 180L, 15L, 771L, 11L),
           PP_col3 = c(1528L, 8333L, 0L, 1560L, 258L),
           PP_col4 = c(1L, 6L, 2L, 2L, 0L),
          CBR_col2 = c(135L, 0L, 0L, 59L, 231L),
          CBR_col3 = c(2372L, 0L, 0L, 119L, 5422L),
          CBR_col4 = c(1L, 0L, 0L, 0L, 4L),
           VO_col2 = c(0L, 0L, 0L, 113L, 184L),
           VO_col3 = c(0L, 0L, 0L, 228L, 4319L),
           VO_col4 = c(0L, 0L, 0L, 0L, 3L),
          AUP_col2 = c(0L, 0L, 0L, 3735L, 0L),
          AUP_col3 = c(0L, 0L, 0L, 7555L, 0L),
          AUP_col4 = c(0L, 0L, 0L, 11L, 0L)
)

Were AUP_col2 is the sum of XBEMB_col2, BC_col2 and UP_col2 of the original dataframe and so on with AUP_col3 (sum the _col3 of each one) and AUP_col4.

Thanks.

This can be done using the mutate function in the tidyverse package. For instance:

library(tidyverse)

df <- df %>%
    mutate(AUP_col2 = XBEMB_col2 + BC_col2 + UP_col2)

The mutate function will create a new column created using the formula specified. In this above instance, the sum of those three columns. Repeat this as needed for the rest of your columns, and then subset the column list to remove any columns you don't need.

I hope this helps!

Great, works fine. Thanks.

This topic was automatically closed 7 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.