[R] Reading in tab (and space) delimited data within a script XXXX

David Winsemius dwinsemius at comcast.net
Thu Jan 19 17:49:10 CET 2012


On Jan 19, 2012, at 11:37 AM, Dan Abner wrote:

> Hello everyone,
>
> I use Bob Muenchen's approach for reading in "in-stream" (to use SAS
> parlance) delimited data within a script. This works great:
>
>
> mystring <-
> "id,workshop,gender,q1,q2,q3,q4
> 1,1,f,1,1,5,1
> 2,2,f,2,1,4,1
> 3,1,f,2,2,4,3
> 4,2, ,3,1, ,3
> 5,1,m,4,5,2,4
> 6,2,m,5,4,5,5
> 7,1,m,5,3,4,4
> 8,2,m,4,5,5,5"
>
> mydata <- read.table( textConnection(mystring),
>   header=TRUE, sep=",",
>   row.names="id", na.strings=" ")
> closeAllConnections()
> mydata
>
> Can anyone suggest a similar approach for reading in tab-delimited or
> single space delimited data? Example data:

You need to read the help page more thoroughly. Either tab or space  
are members of the "whitespace" collection of delimiters which are the  
default separators in read.table(). The would be a problem with spaces  
as na.strngs but you seem to have "-99" in that role below.



?read.table
>
> data3<-
> "OBSNO AGE SEX ALKPHOS LAB CAMMOL PHOSMMOL AGEGROUP
> 21 76 M 84 5 3.2 0.9 3
> 22 76 M  5 2.18 0.84 3
> 23 68 M 82 5 2.15 0.52 1
> 24 69 M 84 5 2.3 1.36 1
> 25 76 F 100 3 25.3 1.07 3
> 26 70 F 90 3 20 0.97 2
> 27 71 F 109 3 22.3 0.94 2
> 28 70 -99 65 3 24.3 1.42 2
> 29 74 F 61 3 25 0.87 2
> 30 74 F 62 3 23.3 0.94 2"
>
> Thanks!
>
> Dan
>
> 	[[alternative HTML version deleted]]

Still not sending plain text, Dan.

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list