可以操纵列的功能

时间:2019-10-14 03:33:05

标签: r

id <- (3,3,6,4)
seq <- (4,7,9,10)
code <- ("009","008","7994","5715","9008","73638967","780092","7994906","0068923") 




providedstring <- c('009','006,'8967','7994')

我尝试使用str_detect和filter。

filter(str_detect(code,providedstring))

这给我一个错误:

longer object length is not a multiple of shorter object length

所需的输出:

id <- (3,6)
seq <- (4,9)
code <- (009,7994906,0068923)

2 个答案:

答案 0 :(得分:0)

您可以这样做

id <- c(3,3,6,4)
seq <- c(4,7,9,10)
code <- c('009','008','7994','5715') 
providedstring <- c('009','006','8967','7994')
# add ^ in front of providedstring to specify we want match only for start
providedstring <- paste0("^",providedstring)
# create dataframe with 3 columns
df <-  data.frame(id=id,seq=seq,code=code)
# filter rows that contain one of the codes in provided string
df %>% 
   filter(str_detect(code,paste(providedstring,collapse="|")))

答案 1 :(得分:0)

现在尝试:

library(tidyverse)
df <- data.frame( 
  id = c(1:9),
  seq = c(9:1),
  code = c("009","008","7994","5715","9008","73638967","780092","7994906","0068923") 
)
providedstring <- c("009","006","8967","7994")
a <- sapply(providedstring, function(x) as.numeric(str_starts(df$code, x))) 
df$yes <- apply(a, 1, max)
df %>% filter(yes ==1)

它将产生:

  id seq    code yes
1  1   9     009   1
2  3   7    7994   1
3  8   2 7994906   1
4  9   1 0068923   1