我有以下代码,但它会抛出以下错误:
the condition has length > 1 and only the first element will be used
显然这意味着我在滥用if (condition) {action}
构造,所以我正在寻找一种更优雅的方法来做到这一点:
if (grepl("Spring", data$Survey)) {data["Season"] <- "Spring"}
if (grepl("Summer", data$Survey)) {data["Season"] <- "Summer"}
if (grepl("Fall", data$Survey)) {data["Season"] <- "Fall"}
基本上,我想输入一个逻辑向量,并且对于每个TRUE
,将一个函数应用于相同长度的数据集(在这种情况下,将字符串值赋给数据帧的该行)
答案 0 :(得分:3)
您可以使用例如 stringr::str_extract
一步完成此操作。如果没有匹配,它将返回<NA>
。
library(dplyr)
library(stringr)
data <- data.frame(Survey = sample(c("it's Spring yay",
"it's Summer yay",
"it's Fall yay"),
20, replace = TRUE))
data <- data %>%
mutate(Season = str_extract(Survey, "(Spring|Summer|Fall)"))
答案 1 :(得分:2)
您可以使用ifelse
作为矢量而不是if
:
data$Season <- ifelse(grepl("Spring", data$Survey),
"Spring",
ifelse(grepl("Summer", data$Survey),
"Summer",
ifelse(grepl("Fall", data$Survey),
"Fall",
"Winter")))
对于data$Survey
的每个元素:首先ifelse
将检查它是否包含&#34; Spring&#34; ,然后将其分配给data$Season
中的相应项目,否则第二个ifelse将检查它是否包含&#34;夏天&#34; ...
答案 2 :(得分:0)
我们可以使用base R
来执行此操作
data$Season <- regmatches(data$Survey, regexpr("\\b(Spring|Survey|Fall)\\b", data$Survey))
data$Season
#[1] "Spring" "Spring" "Fall" "Fall" "Spring" "Spring" "Fall" "Spring" "Fall" "Spring" "Spring"
#[12] "Fall" "Spring" "Fall"
data <- data.frame(Survey = sample(c("it's Spring yay",
"it's Summer yay",
"it's Fall yay"),
20, replace = TRUE))