3 回答

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
fixed==TRUE
"A1|A9|A6"
.
toMatch <- c("A1", "A9", "A6")
matches <- unique (grep(paste(toMatch,collapse="|"), myfile$Letter, value=TRUE))

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊
好的答案,但是不要忘記filter()來自Dplyr:
patterns <- c("A1", "A9", "A6")
>your_df
FirstName Letter
1 Alex A1
2 Alex A6
3 Alex A7
4 Bob A1
5 Chris A9
6 Chris A6
result <- filter(your_df, grepl(paste(patterns, collapse="|"), Letter))
>result
FirstName Letter
1 Alex A1
2 Alex A6
3 Bob A1
4 Chris A9
5 Chris A6

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊
#Returns all items in a list that are not contained in toMatch#toMatch can be a single item or a list of itemsexclude <- function (theList, toMatch){ return(setdiff(theList,include(theList,toMatch)))}#Returns all items in a list that ARE contained in toMatch#toMatch can be a single item or a list of itemsinclude <- function (theList, toMatch){ matches <- unique (grep(paste(toMatch,collapse="|"), theList, value=TRUE)) return(matches)}
- 3 回答
- 0 關(guān)注
- 549 瀏覽
添加回答
舉報(bào)