How to read delimited data as data from a script / document file?

I could not think of exactly what question should have been, if you have a suggestion about what it should be, let me know.

I saw before reading data in a data frame that has a tab or white color in the working script file. For instance:

dat <- SOMETHING( person1 12 15 person2 15 18 person3 20 14 ) 

Say you are capturing data from a website, and just want to record a few things, and this happens like with white space, etc. I could open a text file and save it, and then read it. A library or similar with csv but I'm sure I saw data read in this way and I can’t remember all my life how ...

thanks

+6
source share
1 answer

The "trick" is a text join as the argument to "file" for read.table:

 dat <- read.table(textConnection("person1 12 15 person2 15 18 person3 20 14"), stringsAsFactors=FALSE ) str(dat) 'data.frame': 3 obs. of 3 variables: $ V1: chr "person1" "person2" "person3" $ V2: int 12 15 20 $ V3: int 15 18 14 

The "sep" argument works by default to separate spaces. If you need tabs for highlighting, use sep = "\ t" (after closing with a call to textConnection ).

Edit: this is actually included in a subsequent revision of the base scan function that was given the "text" argument. Now the code may simply be:

 dat <- read.table(text="person1 12 15 person2 15 18 person3 20 14", stringsAsFactors=FALSE ) 

The readLines function still requires the use of textConnection to read from a "character" object, since it does not use scan .

+14
source

Source: https://habr.com/ru/post/896934/


All Articles