lundi 23 novembre 2015

Match Column Vector Strings to Numbers

Here is the code I am working with:

test <- LETTERS[1:18]
Teams <- matrix(sample(test), ncol=3)
Team1 <- (Teams[,1])
Team2 <- Teams[,2]
Team3 <- Teams[,3]

Team1Day1 <- c("l","l","l","l","t","w")
Team2Day1 <- c("w","t","l","l","w","t")
Team3Day1 <- c("l","l","w","w","w","w")



Team1Day1 <- ifelse(Team1Day1=="w",3,ifelse(Team1Day1=="t",1,0))
Team2Day1 <- ifelse(Team2Day1=="w",3,ifelse(Team2Day1=="t",1,0))
Team3Day1 <- ifelse(Team3Day1=="w",3,ifelse(Team3Day1=="t",1,0))

PPG1 <- sum(Team1Day1)/length(Team1Day1)
PPG1

PPG2 <- sum(Team2Day1)/length(Team2Day1)
PPG2 

PPG3 <- sum(Team3Day1)/length(Team3Day1)
PPG3


Trial <- ifelse(test==Team1,PPG1,ifelse(test==Team2,PPG2,PPG3))

Trial

Currently I am getting

  [1] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.6666667
[11] 0.0000000 2.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.6666667 0.0000000

I even tried something like this:

test5 <- ifelse(match(test,Team1)> 0, PPG1,0)
test5
test6 <- ifelse(match(test,Team2) > 0, PPG2, test5)
test6
maybe <- ifelse(match(test,Team2), PPG3, test6)
maybe

That produces [1] 2 2 2 NA NA NA 2 NA NA NA 2 NA NA NA NA 2 NA NA

The second approach seems pretty easy but cannot figure out how to do this, it should be relatively simple. Do you think that maybe I should coerce the dataframe so that the ifelse statement will work? The desired outcome would that in the order that test is listed I would have the corresponding points for example, which I could use the cbind function to create a dataframe like the one below, but getting the numbers in order is my issue.

[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R"
[2]  2   1   2   1   2   0   2   1  ... 

Those numbers are made up and do not correspond to the actual program. Thank you for any help that you can provide it is much appreciated.

Aucun commentaire:

Enregistrer un commentaire