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

David L Carlson dcarlson at tamu.edu
Thu Jan 19 19:57:57 CET 2012


Your first email has

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"

Which produces

> data3
> data3
[1] "OBSNO AGE SEX ALKPHOS LAB CAMMOL PHOSMMOL AGEGROUP\n21 76 M 84 5 3.2
0.9 3\n22 76 M  5 2.18 0.84 3\n23 68 M 82 5 2.15 0.52 1\n24 69 M 84 5 2.3
1.36 1\n25 76 F 100 3 25.3 1.07 3\n26 70 F 90 3 20 0.97 2\n27 71 F 109 3
22.3 0.94 2\n28 70 -99 65 3 24.3 1.42 2\n29 74 F 61 3 25 0.87 2\n30 74 F 62
3 23.3 0.94 2"

With the spaces intact (unlike the example you posted in your second email).


Running read.table gives an error because the second line is short one
variable (no sex apparently):

> read.table(textConnection(data3), header=TRUE, row.names="OBSNO")
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,
: 
  line 2 did not have 8 elements

There might be other errors, read.table stops at the first one. Also don't
use the same variable for the text data and the Rdata (data3). It will just
get confusing.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352




-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Jeff Newmiller
Sent: Thursday, January 19, 2012 11:24 AM
To: Dan Abner; R. Michael Weylandt
Cc: r-help at r-project.org
Subject: Re: [R] Reading in tab (and space) delimited data within a script
XXXX

Cannot see what you are "doing" wrong, since you don't show that. It looks
like you have eliminated all delimiters from your input data. Perhaps your
editor settings are doing something to your data (though usually tabs if
altered become spaces).
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Dan Abner <dan.abner99 at gmail.com> wrote:

>Hi Michael,
>
>Thanks for your responses.
>
>When I do this, I am not successful. What am I doing wrong?
>
>> data3<-
>+ "OBSNOAGESEXALKPHOSLABCAMMOLPHOSMMOLAGEGROUP
>+ 2176M8453.20.93
>+ 2276M52.180.843
>+ 2368M8252.150.521
>+ 2469M8452.31.361
>+ 2576F100325.31.073
>+ 2670F903200.972
>+ 2771F109322.30.942
>+ 2870-9965324.31.422
>+ 2974F613250.872
>+ 3074F62323.30.942"
>>
>>
>> data3<-read.table(textConnection(data3),
>+    header=TRUE,sep=" ",
>+    row.names="OBSNO")
>Error in data[[rowvar]] : attempt to select less than one element
>> closeAllConnections()
>> data3
>[1]
>"OBSNOAGESEXALKPHOSLABCAMMOLPHOSMMOLAGEGROUP\n2176M8453.20.93\n2276M52.180.
843\n2368M8252.150.521\n2469M8452.31.361\n2576F100325.31.073\n2670F903200.97
2\n2771F109322.30.942\n2870-9965324.31.422\n2974F613250.872\n3074F62323.30.9
42"
>>
>
>> data3<-read.table(textConnection(data3),
>+    header=TRUE,sep="\t",
>+    row.names="OBSNO")
>Error in data[[rowvar]] : attempt to select less than one element
>> closeAllConnections()
>> data3
>[1]
>"OBSNOAGESEXALKPHOSLABCAMMOLPHOSMMOLAGEGROUP\n2176M8453.20.93\n2276M52.180.
843\n2368M8252.150.521\n2469M8452.31.361\n2576F100325.31.073\n2670F903200.97
2\n2771F109322.30.942\n2870-9965324.31.422\n2974F613250.872\n3074F62323.30.9
42"
>>
>
>
>
>
>On Thu, Jan 19, 2012 at 11:45 AM, R. Michael Weylandt <
>michael.weylandt at gmail.com> wrote:
>
>> Simply change the sep  = "," argument of read table: " " for a space
>> and "\t" for a tab. E.g., read.table(text = data3, sep = " ", header
>=
>> TRUE)
>>
>> Take a look at ?read.table for more info about the sep argument (In
>> particular the special behavior of the default sep = "")
>>
>> Thanks for the well-posed question and working data.
>>
>> Michael
>>
>> On Thu, Jan 19, 2012 at 11:37 AM, Dan Abner <dan.abner99 at gmail.com>
>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:
>> >
>> > 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]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>>
>http://www.R-project.org/posting-guide.html<http://www.r-project.org/postin
g-guide.html>
>> > and provide commented, minimal, self-contained, reproducible code.
>>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list