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