I want to write a function which takes the log of all columns in a tibble:
test<-tibble(date=c("1992-01-01","1992-01-02"),
value1=c(1,2),
value2=c(3,4))
The difficulty for me is that I would like to append the logarithmic variable as a new column and the variable name should have the following form: e.g.: value_l. In dplyr this should be possible with mutate_all
, but it seems that it is not possible to add a vector there.
test %>%
select(-"date") %>%
mutate_at(funs(!!paste0(colnames(test)[2],"_l") := log(.)))
My code gives me:
Error: The LHS of `:=` must be a string or a symbol
Call `rlang::last_error()` to see a backtrace.
Is there a way to bypass this and stay in the dplyr universe at the same time?
test<-tibble(date=c("1992-01-01","1992-01-02"),
value1=c(1,2),
value2=c(3,4),
value1_l=log(c(1,2)),
value2_l=log(c(3,4)))