Merge columns values

Hi everyone,

Sorry to bother I'm new here and with Rstudio, I've run into some issues. Lemme explain:
image
Here I'd like to merge columns 'conso_gpe1', 'conso_gpe2', 'conso_gpe4' and 'conso_gpe5' by their value. (e.g. the first row would return 222, the 2nd row would return 64... etc but in the same column).

Problem is: I looked up online, I found the command 'cols_merge' and it seems amazing... except it doesn't exist since when I use it Rstudio tells me the command is not found. Here I paste the link from where I found the 'cols_merge' command: cols_merge: Merge data from two or more columns to a single column in rstudio/gt: Easily Create Presentation-Ready Display Tables

Is there a package I need to install that I haven't heard about? Otherwise, I tried some other command, such as 'unite()' found in tidyr, and I also tried the 'factor' command with the HRM package (merging values from columns - #3 by SimonAustria), but it doesn't return the result I'm looking for...

Can someone please help? :grimacing:
Thanks for your time!

you are asking to Sum rather than to Merge ...

cols_merge, wont sum for you, it will stick character strings together , its in the gt package.
unite from tidyr similarly wont sum for you.

a base R way assuming you want to find the row sum of all your columns is rowSums which is in base R.




(example_df <- structure(list(V1 = c(3L, 1L, 8L, 7L, 2L),
                              V2 = c(8L, 3L, 6L,  9L, 5L),
                              V3 = c(4L, 6L, 6L, 6L, 9L), 
                              V4 = c(  10L, 6L, 4L, 2L, 9L)),
                         class = "data.frame", row.names = c(NA, -5L)))


example_df$result_of_sum <- rowSums(example_df)

example_df

However, I would use tidyverse package (or just dplyr) which generally makes my work easier.

library(dplyr)

example_df %>% mutate(
  tidy_result = rowSums(select( .,
                                starts_with("V")
  )))
2 Likes

From your explanation, you want to add up columns, not merge them

library(tidyverse)
df <- mpg %>%
  select(cty, hwy, displ)

df <- df %>%
  # This function merges columns
  unite(col = Merge_of_columns, cty, hwy, displ, remove = FALSE) %>%
  # This function sum up columns
    mutate(Sum_of_columns = cty + hwy, # by name
           Sum_of_all_columns = rowSums(across(where(is.numeric)))) # all numeric
#Note the difference between merge and sum
df
#> # A tibble: 234 x 6
#>    Merge_of_columns   cty   hwy displ Sum_of_columns Sum_of_all_columns
#>    <chr>            <int> <int> <dbl>          <int>              <dbl>
#>  1 18_29_1.8           18    29   1.8             47               95.8
#>  2 21_29_1.8           21    29   1.8             50              102. 
#>  3 20_31_2             20    31   2               51              104  
#>  4 21_30_2             21    30   2               51              104  
#>  5 16_26_2.8           16    26   2.8             42               86.8
#>  6 18_26_2.8           18    26   2.8             44               90.8
#>  7 18_27_3.1           18    27   3.1             45               93.1
#>  8 18_26_1.8           18    26   1.8             44               89.8
#>  9 16_25_1.8           16    25   1.8             41               83.8
#> 10 20_28_2             20    28   2               48               98  
#> # ... with 224 more rows
1 Like

Thanks for your help, it's actually correct :slight_smile:

thank you so much it helped me a lot! problem solved :star_struck:

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.