Code to import data from a Stack overflow query into R

后端 未结 4 919
长情又很酷
长情又很酷 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 04:54

    You can also ask the questioner to use the dput function which dumps any data structure in a way that can be just copy-pasted into R. e.g.

    > zz
      a  b   c
    1 1 11 foo
    2 2 12 bar
    3 3 13 baz
    4 4 14 bar
    5 5 15 foo
    
    > dput(zz)
    structure(list(a = 1:5, b = 11:15, c = structure(c(3L, 1L, 2L, 
    1L, 3L), .Label = c("bar", "baz", "foo"), class = "factor")), .Names = c("a", 
    "b", "c"), class = "data.frame", row.names = c(NA, -5L))
    
    > xx <- structure(list(a = 1:5, b = 11:15, c = structure(c(3L, 1L, 2L, 
    + 1L, 3L), .Label = c("bar", "baz", "foo"), class = "factor")), .Names = c("a", 
    + "b", "c"), class = "data.frame", row.names = c(NA, -5L))
    > xx
      a  b   c
    1 1 11 foo
    2 2 12 bar
    3 3 13 baz
    4 4 14 bar
    5 5 15 foo
    

提交回复
热议问题