Ken Termiso
2007-Oct-26 19:54 UTC
[R] Problems with scan() in a tab-sep .txt file with cells that have '///' (three frontslashes)
Hello all, I'm using the following scan() parameters on a tab-separated text file that was generated by R. temp_file <- scan(file = outfile, sep="\t", what = character(), skip = 1, nlines = 1) The problem is that within some cells, there are cases where there are three frontslashes ( /// ). However, the file itself is tab-separated, and the exact problem is that even if I specify sep="\t" in the argument to scan(), whenever it encounters a /// it overrides the tab separation, and uses the /// as the separator. Then within that column, I see a bunch of /t in the output, and no ///. There are also some cells with two frontslashes ( // ), but scan handles them just fine. I've tried many, many other combinations of parameters, but have not found the right one. I know this shouldn't be a problem, b/c read.csv() and read.table() call scan(), and when calling them, such as: abc <- read.table(outfile, sep="\t", header=T) ...I don't have the problem. The /// show up in the output verbatim, and the /t are used as proper delimiters. If anyone can tell me which parameter I am missing in scan(), I would be very grateful. I'd prefer not to have to use make.names() to handle the slashes. Thanks in advance for any help, -Ken _________________________________________________________________ [[replacing trailing spam]] ilnews [[alternative HTML version deleted]]
Charles C. Berry
2007-Oct-26 22:00 UTC
[R] Problems with scan() in a tab-sep .txt file with cells that have '///' (three frontslashes)
On Fri, 26 Oct 2007, Ken Termiso wrote:> Hello all, > > I'm using the following scan() parameters on a tab-separated text file that was generated by R. > > temp_file <- scan(file = outfile, sep="\t", what = character(), skip = 1, nlines = 1) > > The problem is that within some cells, there are cases where there are > three frontslashes ( /// ). However, the file itself is tab-separated, > and the exact problem is that even if I specify sep="\t" in the argument > to scan(), whenever it encounters a /// it overrides the tab separation, > and uses the /// as the separator. Then within that column, I see a > bunch of /t in the output, and no ///. There are also some cells with > two frontslashes ( // ), but scan handles them just fine. >I do not see that.> ### 10 lines of "//\t///" > cat(file="tmp.dat", rep(paste(c("//","///"),collapse='\t'),10),sep='\n') > scan('tmp.dat',sep='\t',what='a')Read 20 items [1] "//" "///" "//" "///" "//" "///" "//" "///" "//" [10] "///" "//" "///" "//" "///" "//" "///" "//" "///" [19] "//" "///"> > version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 6.0 year 2007 month 10 day 03 svn rev 43063 language R version.string R version 2.6.0 (2007-10-03)>As it says: "PLEASE do ... provide commented, minimal, self-contained, reproducible code." Chuck> I've tried many, many other combinations of parameters, but have not found the right one. > > I know this shouldn't be a problem, b/c read.csv() and read.table() call scan(), and when calling them, such as: > > abc <- read.table(outfile, sep="\t", header=T) > > ...I don't have the problem. The /// show up in the output verbatim, and the /t are used as proper delimiters. > > If anyone can tell me which parameter I am missing in scan(), I would be very grateful. I'd prefer not to have to use make.names() to handle the slashes. > > Thanks in advance for any help, > -Ken > > _________________________________________________________________ > [[replacing trailing spam]] > > ilnews > [[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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901