Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: Reference column name with paste

I am attempting to reference a column in my data frame with paste given a variable name and the column is not being recognized. Below is an example data frame and variable names that I am attempting to work with:

arm <- "ARM"

ex_df <- data.frame(col1 = c("A","B","C"), col2 = c("X","Y","Z"), ARM_1 = c(1,2,3), ARM_2 = c(4,5,6,), ARM_3 = c(7,8,9), ARM_4 = c(0,0,0))

ex_df %>%
    transmute(col1 = col1, col2 = str_to_title(col2), paste0(arm,"_",4) = paste0(arm,"_",1) + paste0(arm,"_",2) + paste0(arm,"_",3)) %>%
    arrange(col1,col2)

Error: unexpected '=' in:
    "col2 = str_to_title(col2),
    paste0(arm,"_",4) = "

The desired output should read ARM_4 = ARM_1 + ARM_2 + ARM_3 and output the sum of each columns values.

Thank you for your help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We can use := with !! – along with rowSums on the columns that starts_with the value stored in arm

library(dplyr)
library(stringr)
ex_df %>%
    transmute(col1 = col1, col2 = str_to_title(col2), 
    !!paste0(arm,"_",4) := rowSums(across(starts_with(arm)), na.rm = TRUE))

-output

 col1 col2 ARM_4
1    A    X    12
2    B    Y    15
3    C    Z    18

NOTE: rowSums would be better compared to + – 1) take care of NA elements with na.rm = TRUE, 2) compact option when there are more than 10 ‘ARM’ columns


In case we want to get the value of the column with paste

ex_df %>%
    transmute(col1 = col1, col2 = str_to_title(col2), 
   !!paste0(arm,"_",4) := .data[[paste0(arm,"_",1)]] + 
                         .data[[paste0(arm,"_",2)]] + 
                        .data[[paste0(arm,"_",3)]]) %>%
    arrange(col1,col2)

-output

 col1 col2 ARM_4
1    A    X    12
2    B    Y    15
3    C    Z    18
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading