我有以下向量:
x <- c("Gpr39", "Myrf", "Gpr35", "Hspb7", "Rbpms", "Slfn2")
提供所需的输入,
input <- c("Myrf", "Rbpms")
我想屏蔽x
中的非输入内容,从而导致:
c("", "Myrf", "", "", "Rbpms", "")
请注意,输出的长度必须与初始向量的长度相同,只有具有非输入内容的输出才用""
替换。
我该如何实现?
答案 0 :(得分:4)
我们可以使用%in%
来检查x
中存在哪些字符串
x[!x %in% input] <- ""
x
#[1] "" "Myrf" "" "" "Rbpms" ""
或者也可以使用replace
replace(x, !x %in% input, "")
#[1] "" "Myrf" "" "" "Rbpms" ""
或者通过match
将不匹配的元素转换为空字符串。
x[is.na(match(x, input))] <- ""
#[1] "" "Myrf" "" "" "Rbpms" ""
答案 1 :(得分:3)
尝试一下:
x=ifelse(x %in% input,input,"")
输出:
[1] "" "Rbpms" "" "" "Myrf" ""
OR
我们可以使用is.element
ifelse(is.element(x,input),input,"")
输出:
[1] "" "Rbpms" "" "" "Myrf" ""
OR
vectorize方法:
x[!is.element(x,input)]<-""
答案 2 :(得分:2)
只是一个补充。尽可能避免使用ifelse
。它会大大减慢速度。 (特别是在处理长向量时)。 ifelse
似乎比其他方法慢10倍。
ronak3
,elrico
,ronak2
,ronak1
或saurabh3
这是我的输入:x[match(x, input, 0L) < 1L] <- "";x
input <- c("Myrf", "Rbpms")
x <- c("Gpr39", "Myrf", "Gpr35", "Hspb7", "Rbpms", "Slfn2")
x <- do.call(c,rep(list(x),99999))
elrico <- function(x) {x[match(x, input, 0L) < 1L] <- "";x}
ronak1 <- function(x) {x[!x %in% input] <- "";x}
ronak2 <- function(x) replace(x, !x %in% input, "")
ronak3 <- function(x) {x[is.na(match(x, input))] <- "";x}
saurabh1 <- function(x) ifelse(x %in% input,input,"")
saurabh2 <- function(x) ifelse(is.element(x,input),input,"")
saurabh3 <- function(x) {x[!is.element(x,input)]<-"";x}
microbenchmark::microbenchmark(elrico(x), ronak1(x), ronak2(x), ronak3(x), saurabh1(x), saurabh2(x) saurabh3(x), times=100)
#Unit: milliseconds
# expr min lq mean median uq max neval cld
# elrico(x) 12.19008 12.55082 20.10380 16.50131 18.89640 146.30629 100 a
# ronak1(x) 14.65382 17.41286 23.43916 20.28819 22.42536 105.78990 100 a
# ronak2(x) 12.89295 13.87701 23.21046 18.76664 21.55648 100.71131 100 a
# ronak3(x) 11.92863 12.85301 16.85720 16.45560 17.92133 99.55643 100 a
# saurabh1(x) 152.20484 159.11873 173.20959 162.78868 170.60385 289.57750 100 b
# saurabh2(x) 150.58224 157.72132 171.83867 162.72981 174.32689 281.26878 100 b
# saurabh3(x) 14.73980 16.27338 22.25143 20.26686 21.76195 109.68922 100 a