Code to import data from a Stack overflow query into R

后端 未结 4 918
长情又很酷
长情又很酷 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:06

    Just want to add this because I now use it regularly and I think it's quite useful. There is a package overflow (install instructions below) that has a function to read copied data frames. Say I begin with an SO post that contains the data shown as the following, but with no dput output.

      Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    1          5.1         3.5          1.4         0.2  setosa
    2          4.9         3.0          1.4         0.2  setosa
    3          4.7         3.2          1.3         0.2  setosa
    4          4.6         3.1          1.5         0.2  setosa
    5          5.0         3.6          1.4         0.2  setosa
    6          5.4         3.9          1.7         0.4  setosa
    

    Now if I copy that data directly, and then run the following

    library(overflow)
    soread()
    # data.frame “mydf” created in your workspace
    #   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    # 1          5.1         3.5          1.4         0.2  setosa
    # 2          4.9         3.0          1.4         0.2  setosa
    # 3          4.7         3.2          1.3         0.2  setosa
    # 4          4.6         3.1          1.5         0.2  setosa
    # 5          5.0         3.6          1.4         0.2  setosa
    # 6          5.4         3.9          1.7         0.4  setosa
    

    I now have a data frame named mydf identical to the one I copied in my global environment, so I don't have to wait for the OP to post a dput of their data frame. I can change the name of the data frame with the out argument, which (obviously) defaults to mydf. There are also a few other useful functions for working with SO posts in the package (like sopkgs(), which installs a package temporarily so you can help with a question about a package that you have not previously installed).

    If you leave library(overflow) in your .Rprofile, then soread() makes pretty quick work of importing data from SO posts.

    overflow is available from GitHub, and can be installed with

    library(devtools)
    install_github("overflow", "sebastian-c")
    

提交回复
热议问题