Jan.Schwanbeck at hydrologie.unibe.ch
2007-Apr-19 14:30 UTC
[R] read.table with more than one sep
Dear List, somebody knows, if the following operation can be done in an easier way? The data rows which should be read into R look like that: 4547;1970.01.01 00:00-1970.01.01 01:00; noData Unitil now we are doing this procedure: 1. Dividing columns which are separated by ";" => temp <- read.table(file ,sep=';', na.strings='noData', strip.white=TRUE) => write(temp, temp.txt) 2. Dividing colums which are separated by "-" => temp <- read.table(temp.txt ,sep='-', na.strings='noData', strip.white=TRUE) => write(temp, temp.txt) 3. Dividing colums which are separated by " " => temp <- read.table(temp.txt ,sep=' ', na.strings='noData', strip.white=TRUE) I can imagine that there should be a way to do this more efficient. Thanks for help in advance. Jan Schwanbeck University of Berne Institute of Geography
Try this: # test data Input <- "4547;1970.01.01 00:00-1970.01.01 01:00; noData 4547;1970.01.01 00:00-1970.01.01 01:00; noData" # replace next line with Lines <- readLines("myfile.dat") Lines <- readLines(textConnection(Input)) Lines <- gsub("[;-]", " ", Lines) read.table(textConnection(Lines)) On 4/19/07, Jan.Schwanbeck at hydrologie.unibe.ch <Jan.Schwanbeck at hydrologie.unibe.ch> wrote:> > Dear List, > > somebody knows, if the following operation can be done in an easier way? > > The data rows which should be read into R look like that: > > 4547;1970.01.01 00:00-1970.01.01 01:00; noData > > Unitil now we are doing this procedure: > > 1. Dividing columns which are separated by ";" > > => temp <- read.table(file ,sep=';', na.strings='noData', > strip.white=TRUE) > => write(temp, temp.txt) > > 2. Dividing colums which are separated by "-" > > => temp <- read.table(temp.txt ,sep='-', na.strings='noData', > strip.white=TRUE) > => write(temp, temp.txt) > > 3. Dividing colums which are separated by " " > > => temp <- read.table(temp.txt ,sep=' ', na.strings='noData', > strip.white=TRUE) > > I can imagine that there should be a way to do this more efficient. > > Thanks for help in advance. > > Jan Schwanbeck > > University of Berne > Institute of Geography > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >