Sorry for the basic question, but I could not find an example in this forum to solve this question. I've tried this one and this one.
I want to change / create a new variable in my data.frame via function in R and tidyverse:
Example:
crivo <- function(x) {
x <<- x %>%
mutate(resp_1 = if_else(MEMO_RE01 == 0,"VN","FP")) %>%
mutate(resp_2 = if_else(MEMO_RE02 == 1,"VP","FN"))
}
crivo(memo_re)
My data.frame name is "memo_re", but I'll use this function to other datasets as well, just by changing the x argument. R is creating a new data.frame named x instead of creating a new variable in "memor_re" (original dataset). In other words, I want to assign a function to do that:
memo_re <- memo_re %>% mutate(resp_1 = if_else(MEMO_RE01 == 0,"VN","FP"))
But I need to change many datasets and because of that, I want to be able to specify which dataset I'll change.
reproducible code
library(tidyverse)
memo_re <- data.frame(MEMO_RE01=rep(c(0,1),100), MEMO_RE02=c(0,1))
crivo <- function(x) {
x <<- x %>%
mutate(resp_1 = if_else(MEMO_RE01 == 0,"VN","FP")) %>%
mutate(resp_2 = if_else(MEMO_RE02 == 1,"VP","FN"))
}
crivo(memo_re)