第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用多模式字符向量的grep

使用多模式字符向量的grep

使用多模式字符向量的grep我試著用grep測(cè)試字符串向量是否存在于另一個(gè)向量中,并輸出存在的值(匹配模式)。我有這樣一個(gè)數(shù)據(jù)框架:FirstName Letter    Alex      A1 Alex      A6 Alex      A7 Bob       A1 Chris     A9 Chris     A6我在“信函”列中找到字符串模式的向量,例如:c("A1", "A9", "A6").我想檢查模式向量中的任何字符串是否存在于“信函”列中。如果是的話,我想要的是唯一值的輸出。問題是,我不知道怎么用grep有多種模式。我試過:matches <- unique (     grep("A1| A9 | A6", myfile$Letter, value=TRUE, fixed=TRUE))但是它給了我0場(chǎng)比賽,這不是真的,有什么建議嗎?
查看完整描述

3 回答

?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊

除了@Marek關(guān)于不包括fixed==TRUE,您還需要在正則表達(dá)式中不包含空格。應(yīng)該是"A1|A9|A6".

你還提到有很多模式。假設(shè)它們?cè)谙蛄恐?/trans>

toMatch <- c("A1", "A9", "A6")

然后,您可以直接從這里創(chuàng)建正則表達(dá)式。

matches <- unique (grep(paste(toMatch,collapse="|"), 
                        myfile$Letter, value=TRUE))


查看完整回答
反對(duì) 回復(fù) 2019-07-04
?
holdtom

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


查看完整回答
反對(duì) 回復(fù) 2019-07-04
?
小怪獸愛吃肉

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊

根據(jù)Brian Digg的文章,以下是篩選列表的兩個(gè)有用函數(shù):

#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)}


查看完整回答
反對(duì) 回復(fù) 2019-07-04
  • 3 回答
  • 0 關(guān)注
  • 549 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)