Code to Import Data from a Stack Overflow Query into R

Code to import data from a Stack overflow query into R

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.

Read whitespace-delimited Stack Overflow data with row numbers directly into R

Or read.table:

df <- read.table(text = "      id cate  result
1 1 yes 1
2 1 yes NA
3 1 no NA
4 2 no NA
5 2 yes 1
6 2 yes NA
7 2 no NA
8 3 no NA
9 3 yes NA
10 3 no NA
11 3 yes 1
12 3 yes NA
13 3 no NA
14 3 yes NA
15 4 yes 1
16 4 yes NA
17 4 yes NA
18 4 no NA
19 4 no NA", header = TRUE)
df
#> id cate result
#> 1 1 yes 1
#> 2 1 yes NA
#> 3 1 no NA
#> 4 2 no NA
#> 5 2 yes 1
#> 6 2 yes NA
#> 7 2 no NA
#> 8 3 no NA
#> 9 3 yes NA
#> 10 3 no NA
#> 11 3 yes 1
#> 12 3 yes NA
#> 13 3 no NA
#> 14 3 yes NA
#> 15 4 yes 1
#> 16 4 yes NA
#> 17 4 yes NA
#> 18 4 no NA
#> 19 4 no NA

Created on 2022-07-29 by the reprex package (v2.0.1)

Accessing a table in a stack overflow (SO) question to use as a dataframe for an answer

We can use soread from overflow after copying the lines

library(overflow)
soread()
#data.frame “mydf” created in your workspace
# v1 v2 v3
#1 A a 1
#2 B b 2
#3 C c 3

str(mydf)
#'data.frame': 3 obs. of 3 variables:
# $ v1: chr "A" "B" "C"
# $ v2: chr "a" "b" "c"
# $ v3: int 1 2 3

The package can be installed from github

source("http://jtilly.io/install_github/install_github.R")
install_github("mrdwab/overflow-mrdwab")

Copy and paste data into R

First, copy (e.g. "⌘ + C") the data set.

Then, paste (e.g. "⌘ + V") to create an R character vector:

x <- " A B C D
1: 2 2 5 3
2: 2 1 2 3
3: 3 4 4 3"

Next, use the read.table() function:

y <- read.table(text = x, header = TRUE)

Done! The data is now in a data frame:

class(y)
[1] "data.frame"

You might also want to check out the dput() function which writes an ASCII text representation of an R object that you can paste into your StackOverflow question or answer.

Import Mutliple Csv File as Data Frame in R

You need to use full.names = T in the list.files function. Then I typically use lapply to load the files in. Also, in my code below I use pattern = "\\.csv" because that's what I needed for this to work with my files.

csv <- getwd()
new_seg <- (list.files(path=csv, pattern="\\.csv", recursive = T, full.names = T))

new_seg_dfs <- lapply(new_seg, read.csv)

Now, new_seg_dfs is a list of data frames.

P.S. seems that you maybe set your working directory beforehand since your files are showing up, but it's always good practice to show the every step you took in these examples.

SQL query with comments import into R from file

Are you sure you can't just use it as is? This works despite taking up multiple lines and having a comment:

> library(sqldf)
> sql <- "select * -- my select statement
+ from BOD
+ "
> sqldf(sql)
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8

This works too:

> sql2 <- c("select * -- my select statement", "from BOD")
> sql2.paste <- paste(sql2, collapse = "\n")
> sqldf(sql2.paste)
Time demand
1 1 8.3
2 2 10.3
3 3 19.0
4 4 16.0
5 5 15.6
6 7 19.8


Related Topics



Leave a reply



Submit