R - Add Quotations For Elements In Vector

R Tutorial – Add Quotations Or Special Characters For Elements In Vector

Outline

    Original Post

    R Tutorial – Add Quotations Or Special Characters For Elements In Vector


    Original Post

    R – Add Quotations Or Special Characters For Elements In Vector


    Adding quotations or special characters to elements in vector, I think this a really useful feature. Especially, if you write SQL in R, this will be handy to you. Like the examples:

    # SQL
    # where colname in ("R", "Python", "SQL")
    vec <- c("R", "Python", "SQL")
    > [1] "R" "Python" "SQL"   
    vec_paste <- paste0(vec, collapse = ",")
    > [1] "R,Python,SQL"
    # If you paste all components directly, elements won't have their own quotations.

    Copy And Paste And Use

    add_quotation_for_vector <- function(input, mark = "'"){
      
      len <- length(input)
      quotation <- rep(mark, times = len)
      return_vec <- paste0(quotation, input, quotation)
      
      return(return_vec)
    }
    # 範例
    vec <- c(123, "ABC", "D")
    vec2 <- add_quotation_for_vector(vec)
    > [1] "'123'" "'ABC'" "'D'" 

    Recommended Posts

    Learn Python And R In DataCamp. Start Your Data Science Career.



    Related Posts

    Aron

    A data scientist working in a retail company, with experience in web design and marketing. Recently dives into cryptocurrency and quantitative investing.

    facebook telegram

    Leave a Reply

    • Required fields are market * .
    • Your email address will not be published.
    • Please ensure your email address is correct, then you will get a notification once a new comment reply.

    Your email address will not be published.