Apply rowSums to selected columns

I'm very new to R and currently struggling to calculate sums per row. Since there are some other columns with meta data I have to select specific columns (i.e. column 2 to 43) for the sum.

I tried this but it only gives "0" as sum for each row without any further error:
SUM_df <- dplyr::mutate(df, "SUM_RQ" = rowSums(dplyr::select(df[,2:43]), na.rm = TRUE))

This code works but then I loose all meta data in the other columns which I want to keep
SUM_df <- df %>%
select(2:43) %>%
mutate("SUMRQ" = rowSums(., na.rm = TRUE))

Is there any option to rewrite the first code and select the columns "inside" rowSums?

I'm sorry for this very basic question but I just can't find the error...


Hi, here are some fixes to your codes:

  1. we should remove the dplyr::select:
    SUM_df <- dplyr::mutate(df, "SUM_RQ" = rowSums((df[,2:43]), na.rm = TRUE))
  2. we can create a new dataframe by merging the original df and the sum column:
    SUM_df = cbind(df, SUMRQ=SUM_df$SUMRQ)

alternatively, we can use rowwise function:
SUM_df = df %>%rowwise() %>%mutate(SUM_RQ = sum(c_across(2:43)))

1 Like

Perfect, thanks! Removing select already helped!

1 Like

Your first suggestion is already perfect and there's no need to create a separate dataframe: the mutate() can add the SUM_RQ column to the existing dataframe, like this:

df <- df %>% mutate("SUM_RQ" = rowSums((df[,2:43]), na.rm = TRUE))
1 Like

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.