[R] How can I read the following complicated table

arun smartpink111 at yahoo.com
Fri Dec 14 04:51:42 CET 2012


Hi,
If it is a dataframe with four columns.
dat1<-read.table(text="
Monday 12 78 89
Tuesday 34 44 67
Wednesday 78 98 2
Thursday 34 55 4
Friday 14 25 13
Monday 18 75 56
Tuesday 28 42 65
",header=FALSE,stringsAsFactors=FALSE)


dat1Mon<-dat1[,-1][dat1[,1]=="Monday",] #rows with first column "Monday"
 dat1Tue<-dat1[,-1][dat1[,1]=="Tuesday",] #rows with first column "Tuesday"
 dat1Tue
#  V2 V3 V4
#2 34 44 67
#7 28 42 65
#You can repeat that for other days


#If the table is like this:
vec1<-readLines(textConnection("Monday 12 78 89
Tuesday 34 44 67
Wednesday 78 98 2
Thursday 34 55 4
Friday 14 25 13
Monday 18 75 56
Tuesday 28 42 65"))
vec1Mon<-unlist(strsplit(gsub("\\D+"," ",vec1[grep("Monday",vec1)]),split=" "))
vec1Mon<-as.numeric(vec1Mon[vec1Mon!=""])
 vec1Mon
#[1] 12 78 89 18 75 56
vec1Tue<-unlist(strsplit(gsub("\\D+"," ",vec1[grep("Tuesday",vec1)]),split=" "))
 vec1Tue<-as.numeric(vec1Tue[vec1Tue!=""])
 vec1Tue
#[1] 34 44 67 28 42 65
#etc.

A.K.


----- Original Message -----
From: jpm miao <miaojpm at gmail.com>
To: r-help <r-help at r-project.org>
Cc: 
Sent: Thursday, December 13, 2012 9:50 PM
Subject: [R] How can I read the following complicated table

Hello,

   I have a table (in a txt file) which look like this:

Monday 12 78 89
Tuesday 34 44 67
Wednesday 78 98 2
Thursday 34 55 4

   Then the table repeats Monday , Tuesday, ... followed by several numbers

   My goal is to read values after the table. My problem is a little more
complicated, but I just present a simpler case for ease of illustration. Is
there any way to ask R to "read several number after you see the word
'Monday' and store somewhere", and read several number after you see the
word 'Tuesday' and store somewhere"?

  Thanks,

miao

    [[alternative HTML version deleted]]

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





More information about the R-help mailing list