Home > Software engineering >  Is there a way to quickly encase a list of words with "quotation marks"
Is there a way to quickly encase a list of words with "quotation marks"

Time:11-15

I am trying to create a vector of words in R, but have a huge list of words, so manually writing "word1", "word2", ..., "word100" would be very tedious.

Given I have the list of words comma separated, is there a way to quickly encase them with quotation marks?

For instance I have:

words = c(apple,bannana,cat,...,dolphin)

but I want

words = c("apple","bannana","cat",...,"dolphin").

CodePudding user response:

If we want to create a character vector from an expression

words <-  scan(text = trimws(str1, whitespace = ".*\\(|\\)"),
    what = "", sep = ",", quiet = TRUE)

data

 str1 <- "words = c(apple,bannana,cat)"

CodePudding user response:

Assuming you have the list on your clipboard:

wordlist <- unname(unlist(read.csv(file="clipboard", header=FALSE))) # Windows
wordlist <- unname(unlist(read.csv(pipe("pbpaste"), header=FALSE))) $ MacOs
wordlist
# [1] "apple"   "bannana" "cat"     "dolphin"

You may get a warning message if there is no line return at the end of the clipboard listing, but you can ignore it.

  • Related