1

I am new to R and programming in general. I have two data frames from which I want to calculate the Win probability from the counts of two different data frames Wins and Losses. I want to check through the list and check whether the values for the score appear in both lists, if they do, I want to perform and operation, if they do not I would like it just to return an NA.

    df W          df L

  score freq  score freq
    5   10     5   10 
    10  10     10  5
    7   2      3   2
    4   1

Here is my function I have written so far:

    test <- function(W, L){
    if (W$score == L$score) {
      total <- W$freq + L$freq
      W$freq / total
    }
  else NA
}

I want the output to be a list of the length of W:

0.5
0.66
NA
NA

This works fine for the first value in the data frame but I get the following error: the condition has length > 1 and only the first element will be used. I have been reading here on StackOverflow that I should use an ifelse function instead as that will loop through all of the rows. However, when I tried this it then had a problem with the two data frame columns being of different lengths. I want to re-use this function over a lot of different data frames and they will always be of different lengths, so I would like a solution for that.

Any help would be much appreciated and I can further clarify myself if it is currently unclear.

Thanks

0mm3
  • 319
  • 2
  • 16

1 Answers1

1

You need to join these two data frames using merge function like this:

W <- data.frame(score=c(1,2,3), freq=c(5,10,15))
L <- data.frame(score=c(1,2,4), freq=c(2,4,8))
merge(W, L, by=c("score"="score"), all=TRUE)

  score freq.x freq.y
1     1      5      2
2     2     10      4
3     3     15     NA
4     4     NA      8

Parameter all set to TRUE means that you want to get all the results from both data frames.

bartektartanus
  • 15,284
  • 6
  • 74
  • 102
  • Awesome, thanks, didn't think of this! – 0mm3 Feb 19 '17 at 10:13
  • Hmm I still have a problem because the two different dataframes hold Wins or Losses, so when merging I get a total value for the freq but I don't know whether some were wins or losses. Any thoughts? I'll update the original question to reflect this. – 0mm3 Feb 19 '17 at 10:18
  • What are you trying to achieve?Edit the question and add the desired result. – bartektartanus Feb 19 '17 at 10:19
  • Look at my answer now. You have `freq.x` which is wins and `freq.y` which is losses. I hope this answers your question? – bartektartanus Feb 19 '17 at 10:50
  • Hi, yes that's great thanks, sorry for the confusion. – 0mm3 Feb 19 '17 at 11:02