3

I have a list of text that I copied from outside of R:

  • Ex: a, b, c, d, e, f, g, h, i, j

I want to just quickly turn each word (in this case letter) to a character string for R to use:

  • Ex: 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'

Is there a function or quick way to turn external text into string characters?

Example:

> a,b,c,d
Error: unexpected ',' in "a,"

> function(a,b,c,d)
[1] 'a','b','c','d'

I understand a simple function likely won't work because it will assume the non-character strings are unknown R objects. But there is likely some trick to make this work?

Note: I know I can do this in other programs (e.g., notepad ++) fairly quickly, but I'm wondering if it's possible to do so quickly in R?

theforestecologist
  • 4,667
  • 5
  • 54
  • 91
  • 2
    You can use `scan` i.e. `scan(text='a, b, c, d, e, f, g, h, i, j',what = "", sep=",", quiet=TRUE)` . If it is stored in a file `scan("yourfile.txt", what ="", sep=",", quiet=TRUE)` – akrun Oct 08 '16 at 17:02
  • 1
    `f <- function(..., simplify = TRUE) sapply(eval(substitute(alist(...))), function(x) if (is.symbol(x)) as.character(x) else eval(x), simplify = simplify); f(a, b, c, d, e, f, g, h, i, j)` works with other things `f(a, one = 1, two, x = rnorm(10))` – rawr Oct 08 '16 at 17:08
  • You would also need `strip.white = TRUE` in `scan()`. – Rich Scriven Oct 08 '16 at 17:50
  • @RichScriven Good call. – theforestecologist Oct 08 '16 at 17:55

2 Answers2

1

Here is something simple:

I first copy a,b,c,d and in R:

z <- readClipboard()
z
[1] "a,b,c,d"

Then you can play around with it. Example:

strsplit(z, split = ",")
[[1]]
[1] "a" "b" "c" "d"

EDIT: This will only work on Windows. Thanks for comment @Gregor.

s_baldur
  • 29,441
  • 4
  • 36
  • 69
  • 1
    I don't know why others are putting answers only in comments. – s_baldur Oct 08 '16 at 17:19
  • 2
    It is silly but I've been guilty of doing that at times. Typically it's because either 1) I didn't feel what I wrote was elaborated enough to be considered a full "answer" or 2) I haven't actually tested the solution. – Dason Oct 08 '16 at 17:35
  • I had this ambiguity I felt that my answer was elaborated enough to answer the question but hardly more elaborated or better than those in the comments. – s_baldur Oct 08 '16 at 18:44
  • 2
    You're doing the right thing. Even in those cases I mention typically writing an answer is more appropriate than a comment but some of us opt to go the comment route because it's safer or we're lazy and don't want to provide an answer we don't 100% stand behind. Answers are the right way to go though so you did the right thing. – Dason Oct 08 '16 at 19:44
1

Here is an easy solution using read.table and the clipboard (as seen here and here):

  1. Copy text or data to clipboard.

  2. Use the following code:

    read.table("clipboard", head = T, sep = z)
    
    • where z is "," for comma delimited, "\t" for tab delimited, etc.

Using scan is a good alternative as well:

scan(text='a, b, c, d, e, f, g, h, i, j',what = "", sep=",", quiet=TRUE, strip.white = TRUE)

(from comments by akrun and Rich Scriven).


The read.table method is quicker and maintains the structure of the text if its a table with headings (e.g., text copied from MS Excel). So if you're copying data with structure, use read.table.

If you're simply copying text (especially if you just want to copy it directly into your script), then the scan method is sufficient and "more transparent."

Community
  • 1
  • 1
theforestecologist
  • 4,667
  • 5
  • 54
  • 91
  • 1
    Note that the "clipboard" string in `read.table` will only work in Windows. – Gregor Thomas Oct 25 '16 at 19:50
  • @Gregor: Thanks. Being a strictly Windows user I had no idea. Do you know a work around for non-Windows users? I'll update my answer accordingly... – theforestecologist Oct 25 '16 at 19:52
  • 2
    My work computer recently switched to a Mac so it's fresh in my mind ;) The best I've found is using the `text` argument for `read.table` as you show with `scan`, just pasting into a string. – Gregor Thomas Oct 25 '16 at 20:01