Is there a way of reading in a file in a way that each line becomes a vector: for example: meals.txt breakfast bacon eggs sausage lunch sandwich apple marsbar crisps dinner chicken rice custard pie I want to read in this file and end up with 3 different vectors, one called breakfast which contains "bacon", "eggs", sausage" One called lunch with "sandwich", "apple"... etc So is there a way to do this with a file like this? Or would I need to transpose the file using something like Perl? And since the vectors are not all of equal length, would I have to also increase the size of the shorter lines by adding NAs? I'm working with a file much bigger than this and this could be a bit of a bother... Kind Regards, Jim -- View this message in context: http://www.nabble.com/Read-in-a-file---produce-independent-vectors-tp18280318p18280318.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2008-Jul-04 14:54 UTC
[R] Re ad in a file - produce independent vectors
Just read them in as whole lines and then split them giving a list of vectors. See ?readLines and ?strsplit Lines <- readLines("meat.txt") strsplit(Lines, " +") This can also be done using strapply in gsubfn which extracts words rather than splitting on delimiters: library(gsubfn) strapply(Lines, "\\w+") More info on gsubfn is at http://gsubfn.googlecode.com On Fri, Jul 4, 2008 at 10:38 AM, jimineep <jamesrperkins at hotmail.com> wrote:> > Is there a way of reading in a file in a way that each line becomes a vector: > for example: > > meals.txt > > breakfast bacon eggs sausage > lunch sandwich apple marsbar crisps > dinner chicken rice custard pie > > I want to read in this file and end up with 3 different vectors, one called > breakfast which contains "bacon", "eggs", sausage" One called lunch with > "sandwich", "apple"... etc > > So is there a way to do this with a file like this? > > Or would I need to transpose the file using something like Perl? And since > the vectors are not all of equal length, would I have to also increase the > size of the shorter lines by adding NAs? I'm working with a file much bigger > than this and this could be a bit of a bother... > > Kind Regards, > > Jim > -- > View this message in context: http://www.nabble.com/Read-in-a-file---produce-independent-vectors-tp18280318p18280318.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. >
Hi r-help-bounces at r-project.org napsal dne 04.07.2008 16:38:08:> > Is there a way of reading in a file in a way that each line becomes avector:> for example: > > meals.txt > > breakfast bacon eggs sausage > lunch sandwich apple marsbar crisps > dinner chicken rice custard pie > > I want to read in this file and end up with 3 different vectors, onecalled> breakfast which contains "bacon", "eggs", sausage" One called lunch with > "sandwich", "apple"... etcYou can read it through read.table("meals.txt", sep="", header=F, fill=T, as.is=T) see ?read.table for info about parameters. You will get data frame and then you can manipulate it easily. It depends what do you want to do next with your food. Regards Petr> > So is there a way to do this with a file like this? > > Or would I need to transpose the file using something like Perl? Andsince> the vectors are not all of equal length, would I have to also increasethe> size of the shorter lines by adding NAs? I'm working with a file muchbigger> than this and this could be a bit of a bother... > > Kind Regards, > > Jim > -- > View this message in context:http://www.nabble.com/Read-in-a-file---produce-> independent-vectors-tp18280318p18280318.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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
on 07/04/2008 09:38 AM jimineep wrote:> Is there a way of reading in a file in a way that each line becomes a vector: > for example: > > meals.txt > > breakfast bacon eggs sausage > lunch sandwich apple marsbar crisps > dinner chicken rice custard pie > > I want to read in this file and end up with 3 different vectors, one called > breakfast which contains "bacon", "eggs", sausage" One called lunch with > "sandwich", "apple"... etc > > So is there a way to do this with a file like this? > > Or would I need to transpose the file using something like Perl? And since > the vectors are not all of equal length, would I have to also increase the > size of the shorter lines by adding NAs? I'm working with a file much bigger > than this and this could be a bit of a bother... > > Kind Regards, > > JimFirst, read in the data file using: Lines <- readLines("meals.txt") That gives you: > Lines [1] "breakfast bacon eggs sausage" [2] "lunch sandwich apple marsbar crisps" [3] "dinner chicken rice custard pie" Note the current object list: > ls() [1] "DF" "Lines" # Now loop over each line, split it # then take the first value and use assign() # to create a vector with that name, containing # the remaining elements for (i in seq(along = Lines)) { Vec <- unlist(strsplit(Lines[i], " +")) assign(Vec[1], Vec[-1]) } Note the current object list: > ls() [1] "breakfast" "DF" "dinner" "i" "Lines" [6] "lunch" "Vec" > breakfast [1] "bacon" "eggs" "sausage" > lunch [1] "sandwich" "apple" "marsbar" "crisps" > dinner [1] "chicken" "rice" "custard" "pie" HTH, Marc Schwartz
On Fri, 4 Jul 2008, jimineep wrote:> > Is there a way of reading in a file in a way that each line becomes a vector: > for example: > > meals.txt > > breakfast bacon eggs sausage > lunch sandwich apple marsbar crisps > dinner chicken rice custard pie > > I want to read in this file and end up with 3 different vectors, one called > breakfast which contains "bacon", "eggs", sausage" One called lunch with > "sandwich", "apple"... etc > > So is there a way to do this with a file like this?There are very many ways. Perhaps one of the simplest is to use readLines() to get a character vector of one element per line, then strsplit() to split the line on whatever the separator used is. E.g. meals <- readLines("meals.txt") m2 <- strsplit(meals, " +") names(m2) <- sapply(m2, `[`, 1) m3 <- lapply(m2, `[`, -1)> m3$breakfast [1] "bacon" "eggs" "sausage" $lunch [1] "sandwich" "apple" "marsbar" "crisps" $dinner [1] "chicken" "rice" "custard" "pie" -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595