Code to import data from a Stack overflow query into R

后端 未结 4 916
长情又很酷
长情又很酷 2020-12-01 04:08

When I try to answer a question in Stack Overflow about R, a good part of my time is spent trying to rebuild the data given as example (unless the question author has been n

4条回答
  •  清歌不尽
    2020-12-01 05:11

    Maybe textConnection() is what you want here:

    R> zz <- read.table(textConnection("a  b   c
    1 11 foo
    2 12 bar
    3 13 baz
    4 14 bar
    5 15 foo"), header=TRUE)
    R> zz
      a  b   c
    1 1 11 foo
    2 2 12 bar
    3 3 13 baz
    4 4 14 bar
    5 5 15 foo
    R> 
    

    It allows you to treat the text as a "connection" from which to read. You can also just copy and paste, but access from the clipboard is more dependent on the operating system and hence less portable.

提交回复
热议问题