I am new to R and programming in general. I have two data frames from which I want to calculate the probability from the counts of two different outcomes. I want to check through the list and check whether the values for x 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
x freq x 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$x == L$x) {
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
Aucun commentaire:
Enregistrer un commentaire