[R] readLines: how to make a data.frame?

David Winsemius dwinsemius at comcast.net
Tue Oct 19 16:04:23 CEST 2010


On Oct 19, 2010, at 7:27 AM, johannes rara wrote:

> I have a text file containing data:
>
> Som text
> ::
> asdf
> @  1  ds  $  5. /*Edmp  */
> @  8  asu  $  3. /*daf*/
> @  8  asdala  $  2. /*asdfa*/
> @  13  astun  $  11. /*daf */
> @  26  dft  $  3. /*asdf */
> @  31  dsfp  $  2. /*asdf  */
> asjk
> asdfö
>
> My intention is to create a dataframe from this data (only rows
> starting @ and not containng string "asdf"). I tried to read this data
> using this code:
>
> g <- readLines("temp.txt")
> g <- grep("^@", g, value=T)
> g <- gsub("[@|$|\\.|/*]", "", g)
>
> How to put this data into a data.frame?

 > read.table(textConnection(g))
   V1     V2 V3    V4
1  1     ds  5  Edmp
2  8    asu  3   daf
3  8 asdala  2 asdfa
4 13  astun 11   daf
5 26    dft  3  asdf
6 31   dsfp  2  asdf

So it looks like you needed to work on your regex pattern selection  
strategy, but at least the conversion to data.frame is now clear.

Perhaps:
 > read.table(textConnection(g[-grep("asdf",g)]))
   V1    V2 V3   V4
1  1    ds  5 Edmp
2  8   asu  3  daf
3 13 astun 11  daf


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

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list