1

I am looking for a way to convert specific words from a list outside of R to a character vector of strings. I need this as I am doing some regex work and the words will be used as filter terms in a data frame.

For instance, let's say I have a series of unquoted words (e.g. If I were to type in the source screen of r-studio):

audi
chevrolet
honda
ford

I want the above to be turned into:

strings = c("audi","chevrolet","honda","ford")

As then, I can use it in the following filter:

mpg %>% filter(manufacturer %in% strings)

The above use is just an example. I'm really looking for a way to turn unquoted text, that has been either entered manually into R or copy-pasted into R, into a comma-separated character vector that can be used for various things. Also, what is the unquoted text that is not a comment in R?

QHarr
  • 83,427
  • 12
  • 54
  • 101
Dasr
  • 777
  • 6
  • 16

2 Answers2

1

Using readLines:

strings  <- readLines(con = textConnection("audi
chevrolet
honda
ford"))

strings
#[1] "audi"      "chevrolet" "honda"     "ford"

We can also use RStudio Multiple Cursors, and see related post: Fastest way to edit multiple lines of code at the same time

zx8754
  • 52,746
  • 12
  • 114
  • 209
0

str_split from stringer is an option too.

You can copy past your string to an r character variable, you can see that \n the Line Feed is you splitting pattern

library(stringr)
st="audi
chevrolet
honda
ford"
cat(st)
#> audi
#> chevrolet
#> honda
#> ford
print(st)
#> [1] "audi\nchevrolet\nhonda\nford"
st2=str_split(st,pattern = "\\n")[[1]]
print(st2)
#> [1] "audi"      "chevrolet" "honda"     "ford"

Created on 2023-04-19 with reprex v2.0.2

Wael
  • 1,640
  • 1
  • 9
  • 20
  • Sorry, so the above is not comma separated right? or is it just that the commas are not shown? – Dasr Apr 19 '23 at 10:04
  • no it is not a comma separated text, it is just the a copy past from your question between 2 quotes, the line feed character (an ASCII character) moves the cursor down to the next line. – Wael Apr 19 '23 at 10:11
  • 1
    So to get it to output comma separated what is best approach? – Dasr Apr 19 '23 at 10:41
  • 1
    Or using base `strsplit(st, split = "\n")[[1]]`. – zx8754 Apr 19 '23 at 10:44
  • @Dasr there is no such thing as "comma separated vector". In the question you mentioned the output would be used within `filter`, then this should work fine: `mpg %>% filter(manufacturer %in% st2)` – zx8754 Apr 19 '23 at 10:46
  • if you have a comma separated output ``"name1,name2,name3"`` you have just to change the pattern argument in ``str_split`` – Wael Apr 19 '23 at 10:47
  • 1
    Thanks, I know it would work fine for that specific question. But as mentioned, I was interested in this more generally. – Dasr Apr 19 '23 at 10:54