2

I am working with the R programming language. Suppose I see data in the following format in an internet browser (e.g. on a stackoverflow question):

weight   height age
1  2998.958 15.26611  53
2  3002.208 18.08711  52
3  3008.171 16.70896  49
4  3002.374 17.37032  55
5  3000.658 18.04860  50
6  3002.688 17.24797  45
7  3004.923 16.45360  47
8  2987.264 16.71712  47
9  3011.332 17.76626  50
10 2983.783 18.10337  42
11 3007.167 18.18355  50
12 3007.049 18.11375  53
13 3002.656 15.49990  42
14 2986.710 16.73089  47
15 2998.286 17.12075  52

Question: Is it possible to bring this data into R and directly make this data into a data frame (without reformatting, or minimum reformatting)?

Obviously, the following statement will not work:

my_data <- data.frame(weight   height age
                      1  2998.958 15.26611  53
                      2  3002.208 18.08711  52
                      3  3008.171 16.70896  49
                      4  3002.374 17.37032  55
                      5  3000.658 18.04860  50
                      6  3002.688 17.24797  45
                      7  3004.923 16.45360  47
                      8  2987.264 16.71712  47
                      9  3011.332 17.76626  50
                      10 2983.783 18.10337  42
                      11 3007.167 18.18355  50
                      12 3007.049 18.11375  53
                      13 3002.656 15.49990  42
                      14 2986.710 16.73089  47
                      15 2998.286 17.12075  52)

At the moment, I am manually doing this, e.g.

weight <- c("2998.958", "3002.208", "3008.171", etc. )
height <- c("15.26611", etc. )
age <- c("53", "52", etc.)

my_data <- data.frame(weight, height, age)

Is there a more "direct" (i.e. faster way) to do this?

Thanks!

stats_noob
  • 5,401
  • 4
  • 27
  • 83

3 Answers3

4

Rather than data.frame. You can pass it to read.table

read.table(text="weight   height age
1  2998.958 15.26611  53
2  3002.208 18.08711  52
3  3008.171 16.70896  49
4  3002.374 17.37032  55
5  3000.658 18.04860  50
6  3002.688 17.24797  45
7  3004.923 16.45360  47
8  2987.264 16.71712  47
9  3011.332 17.76626  50
10 2983.783 18.10337  42
11 3007.167 18.18355  50
12 3007.049 18.11375  53
13 3002.656 15.49990  42
14 2986.710 16.73089  47
15 2998.286 17.12075  52", header=TRUE)

If the data is on your clipboard, you can do

read.table("clipboard", header=TRUE)
MrFlick
  • 195,160
  • 17
  • 277
  • 295
3

The easier option is to copy the lines (Cntrl + C) and then use soread

library(overflow)
soread()
data.frame “mydf” created in your workspace
     weight   height age
1  2998.958 15.26611  53
2  3002.208 18.08711  52
3  3008.171 16.70896  49
4  3002.374 17.37032  55
5  3000.658 18.04860  50
6  3002.688 17.24797  45
7  3004.923 16.45360  47
8  2987.264 16.71712  47
9  3011.332 17.76626  50
...

check the

> str(mydf)
'data.frame':   15 obs. of  3 variables:
 $ weight: num  2999 3002 3008 3002 3001 ...
 $ height: num  15.3 18.1 16.7 17.4 18 ...
 $ age   : int  53 52 49 55 50 45 47 47 50 42 ...

If we want to rename it to a different object, change the out which is by default "mydf"

soread(out = "df1")

as the usage based on ?soread is

soread( sep = "", header = TRUE, stringsAsFactors = FALSE, skipAfterHeader = NULL, out = "mydf" )


It can be installed from the github with

source("http://news.mrdwab.com/install_github.R")
install_github("mrdwab/overflow-mrdwab")
akrun
  • 874,273
  • 37
  • 540
  • 662
1

This is easy in base R with the text= argument of read.table: you need to specify header=TRUE (to indicate that the first row is the column names) and row.names=1 (to indicate that the first column is the row names). It's convenient to put the optional arguments first, "out of order", so that they're visible above the big block of text.

my_data <- read.table(header=TRUE,
row.names = 1,
text="
                         weight   height age
                      1  2998.958 15.26611  53
                      2  3002.208 18.08711  52
                      3  3008.171 16.70896  49
                      4  3002.374 17.37032  55
                      5  3000.658 18.04860  50
                      6  3002.688 17.24797  45
                      7  3004.923 16.45360  47
                      8  2987.264 16.71712  47
                      9  3011.332 17.76626  50
                      10 2983.783 18.10337  42
                      11 3007.167 18.18355  50
                      12 3007.049 18.11375  53
                      13 3002.656 15.49990  42
                      14 2986.710 16.73089  47
                      15 2998.286 17.12075  52
")
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453