I have 2 columns of different size, A and B.
A | B
5 | 4
1 | 2
3 |
How do I say If CellA is < CellB then give me CellB-CellA?
So it calculates:
"5(A1) is bigger than 4(B1) and 2(B2)" so no results
"1(A2) is smaller than 4(B1) and 2(B2)" so we have 3 and 1
"3(A3) is smaller than 4(B1) and bigger than 2(B2)" so we have 1
Result = (3, 1, 1)
The closest I got was:
if (A < B) {B - A}
But that only works with columns of same size, I want each individual cell of Column A to interact with each individual cell of column B. How can I do that?
CodePudding user response:
Since the columns of different sizes then it must be list and not data.frame so the solution :
listAB <- list(A = c(5,1,3) , B = c(4,2))
equ <- function(li){
result <- vector("numeric")
for (x in li$A){
result <- append(result , sapply(li$B , function(y) if(x < y) y - x))
}
unlist(result)
}
equ(listAB)
#> [1] 3 1 1
Created on 2022-05-29 by the reprex package (v2.0.1)