Reputation: 1040
I know that I can use mutate across to multiply one columns by other specific columns like this:
mtcars %>% mutate(across(disp),.*carb)
In my data, both my variables are numeric:
> str(trad_clean_weighted$Q10_1)
num [1:1024] NA NA 0 1 NA 0 0 NA 1 1 ...
- attr(*, "label")= chr "Q10_1.Dance assessed and reported via report card"
> str(trad_clean_weighted$wgt_part1B)
num [1:1024] 9.73 1.9 3.09 3.1 21.42 ...
However, I get this error when I try to multiply the columns:
my_data %>%
mutate(across(Q10_1),.*wgt_part1B)
Error: Problem with `mutate()` input `..2`.
x non-numeric argument to binary operator
i Input `..2` is `. * wgt_part1B`.
I don't understand why I am getting this error when both variables are numeric! :(
Any thoughts?
Upvotes: 1
Views: 1639
Reputation: 887881
It is because the across
got closed after the column name.
my_data %>%
mutate(across(Q10_1, ~ . * wgt_part1B))
If there are vector of column names, specify those within the across
nm1 <- c('Q10_1', 'Q10_2')
my_data %>%
mutate(across(all_of(nm1), ~ .* wgt_part1B))
FOr a single column, we don't need to use across
my_data %>%
mutate(Q10_1 = Q10_1 * wgt_part1B)
Upvotes: 4