Re: [R] Re ad in a file - produce independent vectors

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri, 04 Jul 2008 10:54:40 -0400

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.
>



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. Received on Fri 04 Jul 2008 - 14:57:01 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 Jul 2008 - 16:31:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive