Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Is it possible to 'copy/paste' data into R? [duplicate]

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!

like image 454
stats_noob Avatar asked Jan 24 '26 06:01

stats_noob


1 Answers

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)
like image 81
MrFlick Avatar answered Jan 25 '26 21:01

MrFlick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!