Hello,> > The problem is that I get a the following error bacause anything after the > # is ignored. > > Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, > : > line 6 did not have 500 elements > > R thinks that line 6 has only 2 elements because of the #. >Use 'readLines' instead, followed by 'strsplit'. In the example below the separator is a space. tc <- textConnection( "yes yes yes yes yes yes yes yes yes yes yes yes # yes yes" ) #x <- read.table(tc) # same error: "line 3 did not have 5 elements" x <- readLines(tc) close(tc) strsplit(x, " ") Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/read-table-issue-with-tp4436554p4436737.html Sent from the R help mailing list archive at Nabble.com.
use the 'comment.char' parameter of read.table Sent from my iPad On Mar 1, 2012, at 17:51, Rui Barradas <rui1174 at sapo.pt> wrote:> Hello, > >> >> The problem is that I get a the following error bacause anything after the >> # is ignored. >> >> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, >> : >> line 6 did not have 500 elements >> >> R thinks that line 6 has only 2 elements because of the #. >> > > Use 'readLines' instead, followed by 'strsplit'. > In the example below the separator is a space. > > tc <- textConnection( > "yes yes yes yes yes > yes yes yes yes yes > yes yes # yes yes" > ) > #x <- read.table(tc) # same error: "line 3 did not have 5 elements" > x <- readLines(tc) > close(tc) > strsplit(x, " ") > > Hope this helps, > > Rui Barradas > > > -- > View this message in context: http://r.789695.n4.nabble.com/read-table-issue-with-tp4436554p4436737.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
The # is the default comment character in read.table(), but that can easily be changed:> tc <- textConnection(+ "yes yes yes yes yes + yes yes yes yes yes + yes yes # yes yes" + )> x <- read.table(tc, comment.char="") > xV1 V2 V3 V4 V5 1 yes yes yes yes yes 2 yes yes yes yes yes 3 yes yes # yes yes There's insufficient context here to know if that was actually the original problem, but is an alternate solution for what Rui proposed. Sarah On Thu, Mar 1, 2012 at 5:51 PM, Rui Barradas <rui1174 at sapo.pt> wrote:> Hello, > >> >> The problem is that I get a the following error bacause anything after the >> # is ignored. >> >> Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, >> : >> ? line 6 did not have 500 elements >> >> R thinks that line 6 has only 2 elements because of the #. >> > > Use 'readLines' instead, followed by 'strsplit'. > In the example below the separator is a space. > > tc <- textConnection( > "yes yes yes yes yes > yes yes yes yes yes > yes yes # yes yes" > ) > #x <- read.table(tc) ? # same error: "line 3 did not have 5 elements" > x <- readLines(tc) > close(tc) > strsplit(x, " ") > > Hope this helps, > > Rui Barradas > >-- Sarah Goslee http://www.functionaldiversity.org