开发者

Selecting elements from list

开发者 https://www.devze.com 2023-03-28 09:25 出处:网络
I have a list of names in a list, such as: site<-list(\"site2-site22\" ,\"site2-site45\", \"site4-site2\", \"site6-site2\",

I have a list of names in a list, such as:

site<-list("site2-site22" ,"site2-site45", "site4-site2", "site6-site2",
           "site9-site27", "site20-site150", "site25-site272", "site32-site47",
           "site62-site74", "site272-site280")

From the list, I need to select those elements which has site2 in it, either before or after -

When I use grep command as below:

grep("site2",site,v开发者_Go百科alue=T)

It gives me all values starting from site2, that is my results looks like:

"site2-site22"    "site2-site45"    "site4-site27"    "site9-site27" 
"site20-site150"  "site25-site272"  "site272-site280"

How can i only select site2 from the list?


I'm sure there are tons of other ways to do this with regular expressions, but this simple one works:

grep("^site2-|-site2$",site,value=T)


grep("site2-|-site2$",site,value=T)
0

精彩评论

暂无评论...
验证码 换一张
取 消