I tried this;
new_data $Ethnicity_dummy = as.numeric(new_data$Ethnicity == "White"|"White British"| "White Other")
but I get this error message;
new_data $Ethnicity_dummy = as.numeric(new_data$Ethnicity == "White"|"White British"| "White Other")
Error in new_data$Ethnicity == "White" | "White British" :
operations are possible only for numeric, logical or complex types
CodePudding user response:
You are using the "or" (|
) function incorrectly - if you wanted to use the or notation, you would have to specify the variable for each time (i.e. new_data$Ethnicity == "White"|new_data$Ethnicity =="White British"...
)
A much easier way is %in%
- try using:
new_data$Ethnicity_dummy = as.numeric(new_data$Ethnicity %in% c("White","White British","White Other"))
CodePudding user response:
library(tidyverse)
new_data <- tibble(
ethnicity = sample(c("white", "white british", "white other"),
10, replace = TRUE)
)
# A tibble: 10 x 1
ethnicity
<chr>
1 white
2 white
3 white british
4 white other
5 white british
6 white british
7 white british
8 white british
9 white
10 white
new_data %>%
mutate(dummy = ethnicity %>%
as_factor() %>%
as.numeric())
# A tibble: 10 x 2
ethnicity dummy
<chr> <dbl>
1 white 1
2 white 1
3 white british 2
4 white other 3
5 white british 2
6 white british 2
7 white british 2
8 white british 2
9 white 1
10 white 1