My dataframe currently looks like this:
employment020406 <- list(emp_02, emp_04, emp_06)
I want to create two new variables called agri_work
and TAL
in each dataset:
emp_02$agri_work <- ifelse(emp_02$industry == 1 | emp_02$industry == 2 | emp_02$industry == 5, 1, 0)
emp_04$agri_work <- ifelse(emp_04$industry == 1 | emp_04$industry == 2 | emp_04$industry == 5, 1, 0)
emp_06$agri_work <- ifelse(emp_06$industry == 1 | emp_06$industry == 2 | emp_06$industry == 5, 1, 0)
emp_02$tal <- ifelse(emp_02$industry == 17 | emp_02$industry == 18 | emp_02$industry == 19, 1, 0)
emp_04$tal <- ifelse(emp_04$industry == 17 | emp_04$industry == 18 | emp_04$industry == 19, 1, 0)
emp_06$tal <- ifelse(emp_06$industry == 17 | emp_06$industry == 18 | emp_06$industry == 19, 1, 0)
Note that industry
exists in all 3 dataframes in the list.
Is there a way I can loop the ifelse
function so that I can create the new variables in each dataframe without having to repeat this action many times?
Thank you very much.
CodePudding user response:
You can index into each element of employment020406
using seq_along()
. (You can also use df$var %in% c(a, b, c)
instead of df$var == a | df$var == b | df$var == c
.)
for (i in seq_along(employment020406)) {
employment020406[[i]]$agri_work <- ifelse(employment020406[[i]]$industry %in% c(1, 2, 5), 1, 0)
employment020406[[i]]$tal <- ifelse(employment020406[[i]]$industry %in% 17:19, 1, 0)
}
CodePudding user response:
If you only want to change employment020406
, you can do this:
employment020406 <- lapply(employment020406, function(x) {
x %>%
mutate(tal = as.numeric(industry %in% c(17,18,19)),
agri_work = as.numeric(industry %in% c(1,2,5))
)
})
However, note that this will not change the underlying objects emp_02
, emp_04
and emp_06
. If you want to change those, you could do this:
employment020406 <- list("emp_02", "emp_04", "emp_06")
for(f in employment020406) {
assign(f,get(f) %>%
mutate(tal = as.numeric(industry %in% c(17,18,19)),
agri_work = as.numeric(industry %in% c(1,2,5)))
)
}