Sam
Sam

Reputation: 19

How to create a function (R Programming) that is relevant for this example

I have a homework / assignment question that needs to be looked at and if you could please provide me with some help, it will be much appreciated.

I have a file something like this shown below: I've only selected the first 20 lines as there is more than 250 lines.

The following line tells us that "NZ1" crossed the start line (at line 14 - shown below)

11:10:01 NZ1 Crossed Start Line

and the following is the finish line (at line 109 - not shown below)

11:35:16 NZ1 Crossed Finished Line

What I need to do is to create a function called getTimes(), that takes the name of a boat (e.g. NZ1, US1 is a boat) and returns the start and finish times for that boat.

txtFile <- readLines("130901_chatter.txt")

txtFile[1:20]

[1] "Chatter Log, Sun 1 Sept 2013"
[2] "Red Bull Youth AC, Races 1 & 2"
[3] ""
[4] "10:35:59 MDS redbull yacht data is public"
[5] "10:44:47 TIM Red Bull Youth America's Cup Race Day 1, Sun 1 Sep 2013"
[6] "10:45:03 WND Wind 255 at 14.2 kts, 14.2 kts peak"
[7] "10:45:41 TIM Race 1 starts at 11:10 PDT, 10 yachts racing, 7 legs"
[8] "10:46:58 GER Yankee Flag"
[9] "10:47:12 UMP GER No Penalty"
[10] "10:55:03 WND Wind 255 at 14.5 kts, 16.7 kts peak"
[11] "11:05:03 WND Wind 245 at 13.8 kts, 14.9 kts peak"
[12] "11:07:00 Race 1 (GER,SUI,NZ2,US1,US2,POR,FRA,AUS,NZ1,SWE): Warning, 3:00 until start" [13] "11:10:00 Race 1 (GER,SUI,NZ2,US1,US2,POR,FRA,AUS,NZ1,SWE): Started"
[14] "11:10:01 NZ1 Crossed Start Line"
[15] "11:10:01 US1 Crossed Start Line"
[16] "11:10:01 US2 Crossed Start Line"
[17] "11:10:02 FRA Crossed Start Line"
[18] "11:10:02 GER Crossed Start Line"
[19] "11:10:02 SUI Crossed Start Line"
[20] "11:10:02 NZ2 Crossed Start Line"

Could you please check my code below?

startResults <- function(filenames){
filess <- readLines(filenames)
startLines <- grep("NZ1 Cross", filess)
startText <- filess[startLines] 
leelee <- startText[1:2]
ohSeung<-substr(leelee, 1,9)
}

crossStart<-startResults("130901_chatter.txt")

getTimes<-function(raceIDs){
raceIDs = crossStart
return(raceIDs)
}
getTimes("NZ1")

If I run the code above, I get the result for NZ1. However, I need to get the result for other boats as well but not too sure how to get it.

Oh, and this is how my raceIDs look like:

raceIDs
[1] "GER" "SUI" "NZ2" "US1" "US2" "POR" "FRA" "AUS" "NZ1" "SWE"

I should be getting something like this if the function is correct (start time and finish time of the boat):

> getTimes("NZ1")
[1] "11:10:01" "11:35:16"

> getTimes("US1")
[1] "11:10:01" "11:33:29"

> getTimes("NZ2")
[1] "11:10:02" "11:34:12"

I believe my function only calculates "NZ1" but not any other boats.

Any helps would be much appreciated!

Upvotes: 0

Views: 161

Answers (1)

IRTFM
IRTFM

Reputation: 263301

Oboy! ... a sailing question! The principle here is to identify the patterns in the text that define a useful line and then segment the line. Take a look at

"11:10:01 NZ1 Crossed Start Line" and those that follow and then see that all the lines with useful data have "Crossed" in them. And they have a pattern for time and a pattern for the team acronym. First create a logical vector that is a "start line" for a particular boat using grepl. Make it more general than you have done so far so you will be able to pass that vector of boat ID's using sapply to the function:

# You already have the text in memory, so no need to load it every time
startResults <- function( boatID){
     startLines <- grepl(paste(boatID, "Cross", sep=" "), txtFile)
     startText <- txtFile[startLines] 
     ## leelee <- startText[1:2]  # that looks wrong
     substr(startText, 1,9)
}

There are probably slicker ways to do this all with regex patterns but this is a start. You now get to make a corresponding end-times functions and string the rest of it together to answer the question.

Upvotes: 1

Related Questions