R - Add Quotations For Elements In Vector

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:

# 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)

# 範例
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.


Data analyst, but building WordPress websites, having experience in digital marketing. Recently dives into fighting and Python quantitative investment.

Content Protection by DMCA.com

Leave a Reply

Your email address will not be published. Required fields are marked *