遍历多个字符列以派生一个新变量
Loop over multiple character columns to derive a new variable
你能帮我写一段 R 中的代码吗?我想派生一个新变量,其值为 Yes 或 No,但是它需要检查字符变量 a1、a2、a3、a4 以及任何变量值为 'yellow' 那么可用变量应该是 yes else no.
a1 <- c('orange','red')
a2 <- c('red','yellow')
a3 <- c('black','orange')
a4 <- c('red','brown')
testa <- data.frame(a1,a2,a3,a4)
dplyr
您可以使用 rowwise
和 c_across
:
testa %>%
rowwise() %>%
mutate(available = ifelse(any(c_across(a1:a4) == "yellow"), "yes", "no"))
a1 a2 a3 a4 available
<chr> <chr> <chr> <chr> <chr>
1 orange red black red no
2 red yellow orange brown yes
基础 R
使用apply
:
testa$available <- apply(testa, 1, function(x) ifelse(any(x == "yellow"), "yes", "no"))
a1 <- c('orange','red')
a2 <- c('red','yellow')
a3 <- c('black','orange')
a4 <- c('red','brown')
df <- data.frame(a1,a2,a3,a4)
library(tidyverse)
df %>%
mutate(available = ifelse(rowSums(. == "yellow") > 0, "yes", "no"))
#> a1 a2 a3 a4 available
#> 1 orange red black red no
#> 2 red yellow orange brown yes
library(data.table)
setDT(df)[, available := ifelse(rowSums(.SD == "yellow") > 0, "yes", "no")][]
#> a1 a2 a3 a4 available
#> 1: orange red black red no
#> 2: red yellow orange brown yes
由 reprex package (v2.0.1)
创建于 2022-01-19
您可以只连接每行中的单元格并在结果字符串中查找字符 "yellow"
:
> grepl("yellow",paste(as.data.frame(t(testa))))
[1] FALSE TRUE
然后您可以使用此生成的逻辑向量将标签放入新列中:
testa$available = c("no","yes")[grepl("yellow",paste(as.data.frame(t(testa))))+1]
这将导致 data.frame:
> testa
a1 a2 a3 a4 available
1 orange red black red no
2 red yellow orange brown yes
(如果你想要"yes"
不包含黄色的线,只需在向量c("no","yes")
中翻转它们)
你能帮我写一段 R 中的代码吗?我想派生一个新变量,其值为 Yes 或 No,但是它需要检查字符变量 a1、a2、a3、a4 以及任何变量值为 'yellow' 那么可用变量应该是 yes else no.
a1 <- c('orange','red')
a2 <- c('red','yellow')
a3 <- c('black','orange')
a4 <- c('red','brown')
testa <- data.frame(a1,a2,a3,a4)
dplyr
您可以使用 rowwise
和 c_across
:
testa %>%
rowwise() %>%
mutate(available = ifelse(any(c_across(a1:a4) == "yellow"), "yes", "no"))
a1 a2 a3 a4 available
<chr> <chr> <chr> <chr> <chr>
1 orange red black red no
2 red yellow orange brown yes
基础 R
使用apply
:
testa$available <- apply(testa, 1, function(x) ifelse(any(x == "yellow"), "yes", "no"))
a1 <- c('orange','red')
a2 <- c('red','yellow')
a3 <- c('black','orange')
a4 <- c('red','brown')
df <- data.frame(a1,a2,a3,a4)
library(tidyverse)
df %>%
mutate(available = ifelse(rowSums(. == "yellow") > 0, "yes", "no"))
#> a1 a2 a3 a4 available
#> 1 orange red black red no
#> 2 red yellow orange brown yes
library(data.table)
setDT(df)[, available := ifelse(rowSums(.SD == "yellow") > 0, "yes", "no")][]
#> a1 a2 a3 a4 available
#> 1: orange red black red no
#> 2: red yellow orange brown yes
由 reprex package (v2.0.1)
创建于 2022-01-19您可以只连接每行中的单元格并在结果字符串中查找字符 "yellow"
:
> grepl("yellow",paste(as.data.frame(t(testa))))
[1] FALSE TRUE
然后您可以使用此生成的逻辑向量将标签放入新列中:
testa$available = c("no","yes")[grepl("yellow",paste(as.data.frame(t(testa))))+1]
这将导致 data.frame:
> testa
a1 a2 a3 a4 available
1 orange red black red no
2 red yellow orange brown yes
(如果你想要"yes"
不包含黄色的线,只需在向量c("no","yes")
中翻转它们)