Reputation: 1
I'm working on an employee attrition dataset. There is a column named attrition and it contains 1470 yes and no values. I want to convert these yes and no values to 1 and 0. I installed the dplyr package in R to use the mutate function but I'm having trouble converting. Here's what I tried:
clean %>% mutate(empAttrcopy$attrition=ifelse(empAttrcopy$attrition=="NO",0,1)) Error: unexpected '=' in "clean %>% mutate(empAttrcopy$attrition="
Upvotes: 0
Views: 2253
Reputation: 39
Try this:
empAttrcopy$attrition=ifelse(empAttrcopy$attrition=="YES", 1 , 0)
empAttrcopy$attrition1=ifelse(empAttrcopy$attrition=="YES", 1 , 0)
I usually add new columns, to have the original variable attrition
and newly created attrition1
for comparison. YES=1, NO=0.
Cheers!
Upvotes: 0
Reputation: 1
You can just use the ifelse
function available on the base
package. This allows an expression to be evaluated and assigned values as appropriate.
a = c(5,7,2,9)
(a %% 2 == 0,"even","odd")
[1] "odd" "odd" "even" "odd"
Upvotes: 0
Reputation: 1357
I suspect you have a dataframe called "empAttrcopy", want to dummy code the column "attrition" and store it to a new dataframe called "clean".
When using dplyr
, the pipe %>%
(originally from magrittr
) evaluates what's on the left side of it and feeds the result as input to the next function on the right. So if you want to mutate a column within "empAttrcopy", you need to put it before the first %>%
. When having done that, R knows that you are working with "Attrition", so there's no need to backreference it after piping (i.e. no need for $
).
empAttrcopy %>% mutate(attrition = ifelse(attrition == "No", 0, 1))
If you want to store the output of your mutated column to a new dataframe called "clean", do so by assigning it by using <-
at the start or ->
at the end.
clean <- empAttrcopy %>% mutate(attrition = ifelse(attrition == "No", 0, 1))
# or
empAttrcopy %>% mutate(attrition = ifelse(attrition == "No", 0, 1)) -> clean
Last but not least, it's always better to store the output in a new column (e.g. save it to "attrition_dummy"), so that you don't lose any data.
clean <- empAttrcopy %>% mutate(attrition_dummy = ifelse(attrition == "No", 0, 1))
Upvotes: 1
Reputation: 11046
Assuming attrition
contains only "YES" or "NO" values, this should work. First some reproducible data:
set.seed(42)
attrition <- sample(c("YES", "NO"), 15, replace=TRUE)
attrition
# [1] "YES" "YES" "YES" "YES" "NO" "NO" "NO" "NO" "YES" "NO" "YES" "NO" "YES" "NO" "YES"
attrition <- as.numeric(attrition=="YES")
attrition
# [1] 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1
I've left off the empAttrcopy$
prefix for simplicity.
Upvotes: 3