Yong Wang
2010-Aug-30 04:18 UTC
[R] Read in a all-character file and specify field separator and records separator
Dear list I used to use python or awk do preliminary process and then feed into R. In some circumstances, the data transmission becomes quite a pain. I am wondering if there is a convenient way to read in R text file (not data, text file in common sense) and specify field separator and records separator, so the whole work can be reduced to one-stop shopping. or simply, is there one simple way to read in the text file with each row in a single column. scan(....sep="\n") does not work as expected. Thanks yong
Joshua Wiley
2010-Aug-30 05:29 UTC
[R] Read in a all-character file and specify field separator and records separator
On Sun, Aug 29, 2010 at 9:18 PM, Yong Wang <wangyong1 at gmail.com> wrote:> Dear list > > I used to use python or awk do preliminary process and then feed into > R. In some circumstances, the data transmission becomes quite a pain. > I am wondering if there is a convenient way to read in R text file > (not data, text file in common sense) and specify field separator and > records separator, so the whole work can be reduced to one-stop > shopping. > or simply, is there one simple way to read in the text file with each > row in a single column. scan(....sep="\n") does not work as expected.Do either of these do what you want? readLines(con = file("example.txt", open = "r")) read.table(file = "example.txt", sep = "\n") Both throw a warning in a little text file I made, but both read each line into one column. Josh> > Thanks > > yong > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
David Winsemius
2010-Aug-30 05:34 UTC
[R] Read in a all-character file and specify field separator and records separator
On Aug 30, 2010, at 12:18 AM, Yong Wang wrote:> Dear list > > I used to use python or awk do preliminary process and then feed into > R. In some circumstances, the data transmission becomes quite a pain. > I am wondering if there is a convenient way to read in R text file > (not data, text file in common sense) and specify field separator and > records separator, so the whole work can be reduced to one-stop > shopping. > or simply, is there one simple way to read in the text file with each > row in a single column. scan(....sep="\n") does not work as expected. >You should not need to use sep=\n or any separator for a single column output. If what= is not specified you will just get a numeric vector, but if it's text, you need to specify "character". Try the obvious modifications to this: char.vec <- scan(file="/filepath/filename.ext", what="character" ) Multiple columns require something like what=list("character", "numeric", "logical"), but you could do what the rest of us use because it's all wrapped up with sensible defaults (with the possible exception of stringsAsFactors ) in the read.table function. -- David. .
Possibly Parallel Threads
- read.table() with "\t" as seperator, all other programs report equal fields each row, read.table() returns unequal row length error
- How to do varimax rotation for principal component based factor analysis, any packages?
- what does this err mean and how to solve it? Error in file(file, ifelse(append, "a", "w"))
- Why a multi column, tab delimited file has only one column after reading in with read.table specification sep="\t"
- How to speed up the for loop by releasing memeory