Reputation: 443
I have a data frame:
df <- data.frame(A=c(10, 20, 30, 20),
B=c(0, 10, 20, 10),
C=c(11, 22, 21, 12),
D=c(13, 11, 33, 15))
A B C D
10 0 11 13
20 10 22 11
30 20 21 33
20 10 12 15
and a function to get the index of the number in a pair closest to a number of interest:
comp <- function(x, y) {
# x = number of interest, y = vector of 2 numbers for comparison)
ind <- which(abs(y-x)==min(abs(y-x)))
if (length(ind)==2) {
ind <- 3
}
return(ind)
}
(The if statement is for when the number is smack in the middle of the two numbers, ex. 15 compared to 10 and 20).
I would like to change columns C & D to the index for which the number is closest using my function (1 for A or 2 for B):
A B C D
10 0 1 1
20 10 1 2
30 20 2 1
20 10 2 3
I'm not sure how to call in columns A and B as arguments for the function. I've tried mutate_at(df, 3:4, funs(comp), c(df$A, df$B))
, but that returns:
A B C D
10 0 3 6
20 10 3 6
30 20 3 6
20 10 3 6
Doesn't have to be a tidyr solution, whatever works! Thanks
Upvotes: 2
Views: 1984
Reputation: 5415
I changed around your function a little bit in order for vectorization to work. It also only accepted 2 values, when you were looking to compare 1 value with 2 others, so 3 arguments would be needed:
comp <- function(val, x, y){
case_when(
abs(val - x) < abs(val - y) ~ 1,
abs(val - x) > abs(val - y) ~ 2,
TRUE ~ 3)
}
df %>%
mutate_at(vars(C,D), comp , .$A, .$B)
A B C D
1 10 0 1 1
2 20 10 1 2
3 30 20 2 1
4 20 10 2 3
Upvotes: 2