我在R中有Yes和No列,我需要将no转换为0并将yes转换为1 到目前为止,我有
clean %>% mutate(flight=ifelse(flight=="NO", 0,1))
但是,事实证明有些值'N','n'或'no'已全部转换为1。如何将所有这些变体也转换为0?
答案 0 :(得分:3)
如果我们需要将多个值“ N”,“ n”,“ no”,“ NO”和所有其他值为“ Yes”的值转换为0和1,请使用substr
获取第一个字符,然后进行更改与大写字母(toupper
进行比较(!=
)与“ N”并将其强制转换为二进制(as.integer
)
library(dplyr)
clean %>%
mutate(flight = as.integer(toupper(substr(flight, 1, 1)) != "N"))
注意:假设列中只有“是”,“否”,“否”,“ N”,“ n”作为值
clean <- tibble(flight = c("No", "Yes", "YES", "Y", "no",
"No", "NO", "Y", "n", "y", "No"))
答案 1 :(得分:2)
在grepl()
搜索中使用条件library(tidyverse)
clean <-
tibble(
flight =
c("No", "Yes", "YES", "Y", "no", "No", "NO", "Y", "n", "y", "No"))
clean %>%
mutate(
flight =
ifelse(
test = grepl(pattern = "N|n", x = flight),
yes = 0,
no = 1))
Routes
答案 2 :(得分:1)
您也可以像下面这样使用substr
来代替startswith
:
library(dplyr)
clean <- tibble(flight = c("No", "Yes", "YES", "Y", "no",
"No", "NO", "Y", "n", "y", "No"))
clean %>% mutate(flight = as.integer(x = startsWith(tolower(flight), "y")))
答案 3 :(得分:1)
使用来自@akrun的样本数据,可能有另一种dplyr
和grepl()
的可能性:
clean %>%
mutate(temp = (!grepl("n", flight, ignore.case = TRUE)) * 1)
flight
<int>
1 0
2 1
3 1
4 1
5 0
6 0
7 0
8 1
9 0
10 1
11 0
或者只是base R
一样:
clean$flight <- (!grepl("n", clean$flight, ignore.case = TRUE)) * 1
或者:
clean %>%
mutate(flight = as.integer(!grepl("n", flight, ignore.case = TRUE)))
或仅以基数R相同:
clean$flight <- as.integer(!grepl("n", clean$flight, ignore.case = TRUE))