Sam
Sam

Reputation: 19

How to create a matrix using this data

I have encountered a problem while I was doing my homework/assignment and I would appreciated your expertise if anyone is willing to help :)

I have a file called raceIDs (which looks like this):

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

I have also created a function called:

getTimes()

For example, if I enter:

> getTimes("GER")
[1] "11:10:02" "11:35:05"  #Shows the starting and finishing times

What I need to do here is to create a matrix showing the starting and ending times for all the countries.

For example, I should be getting a matrix of the following result:

> raceTimes
    start        finish
GER "11:10:02"   "11:35:05"
SUI "11:10:02"   "11:35:22"
#and so on....

My attempt:

> GER <- getTimes("GER")
> SUI <- getTimes("SUI")
> NZ2 <- getTimes("NZ2")
> US1 <- getTimes("US1")
> US2 <- getTimes("US2")
> POR <- getTimes("POR")
> FRA <- getTimes("FRA")
> AUS <- getTimes("AUS")
> NZ1 <- getTimes("NZ1")
> SWE <- getTimes("SWE")

> allTimes<-c(GER, SUI, NZ2, US1, US2, POR, FRA, AUS, NZ1, SWE)
> raceTimes <- matrix(allTimes, ncol=2)
> rownames(raceTimes) <- paste(raceIDs)
> colnames(raceTimes) <- c("start", "finish")

> raceTimes
    start      finish    
GER "11:10:02" "11:10:02"
SUI "11:35:05" "11:34:31"
NZ2 "11:10:02" "11:10:02"
US1 "11:35:22" "11:34:45"
US2 "11:10:02" "11:10:03"
POR "11:34:12" "11:36:48"
FRA "11:10:01" "11:10:01"
AUS "11:33:29" "11:35:16"
NZ1 "11:10:01" "11:10:03"
SWE "11:36:05" "11:35:08"

As you can see above, my matrix is not correct. For example, the finish result for "GER" ("11:35:05") is showing as the start time for "SUI".

Any suggestions please?

Upvotes: 1

Views: 73

Answers (1)

Blue Magister
Blue Magister

Reputation: 13363

Common mistake: creating a matrix or array out of a vector fills in column-major order, as the introduction manual states::

The values in the data vector give the values in the array in the same order as they would occur in FORTRAN, that is “column major order,” with the first subscript moving fastest and the last subscript slowest.

This can be observed by

matrix(1:4, ncol = 2)

#      [,1] [,2]
# [1,]    1    3
# [2,]    2    4

The most immediate fix is to add byrow = TRUE to your matrix call.

matrix(1:4, ncol = 2, byrow = TRUE)

#      [,1] [,2]
# [1,]    1    2
# [2,]    3    4

raceTimes <- matrix(allTimes, ncol=2, byrow = TRUE)

A better idea would be to use rbind upstream to do the joining without needing to string together a vector:

raceTimes <- rbind(GER, SUI, NZ2, US1, US2, POR, FRA, AUS, NZ1, SWE)

A better idea would be to use looping and sapply instead of typing out getTimes for each element in raceID:

raceTimes <- sapply(raceID, getTimes)
raceTimes <- t(raceTimes)

Upvotes: 2

Related Questions