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]]
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.
Rainer Schuermann
2012-Dec-14 04:44 UTC
[R] How can I read the following complicated table
What have you tried so far that did not work, and what do you want the result of your reading the text file look like? What is "store somewhere"? Why does> myDF <- read.table( "myData.txt" )which gives you> myDFV1 V2 V3 V4 1 Monday 12 78 89 2 Tuesday 34 44 67 3 Wednesday 78 98 2 4 Thursday 34 55 4 as a starting point, not suffice? Rgds, Rainer On Friday 14 December 2012 10:50:56 jpm miao wrote:> 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.