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

Pass a function input as column name to data.frame function

I have a function taking a character input. Within the function, I want to use the data.frame() function. Within the data.frame() function, one column name should be the function’s character input.

I tried it like this and it didn’t work:

frame_create <- function(data, character_input){

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


some_vector <- c(1:50)

temp_frame <- data.frame(character_input = some_vector, …)

return(temp_frame)

}

>Solution :

Either use, names to assign or with setNames as = wouldn’t allow evaluation on the lhs of =. In package functions i.e tibble or lst, it can be created with := and !!

frame_create <- function(data, character_input){
    some_vector <- 1:50
    temp_frame <- data.frame(some_vector)
    names(temp_frame) <- character_input
    return(temp_frame)
}
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