Alex Rivera | Logout

Does R have a wildcard expression (such as an asterisk (*))?

Asked 2011-11-17T21:29:04.373
17

I hope I am not missing something obvious but here is my question:

Given data like this

Data1=
Name       Number
AndyBullxxx    12
AlexPullxcx    14
AndyPamxvb     56
RickRalldfg    34
AndyPantert    45
SamSaltedf     45

I would like to be able to pull out all of the rows starting with "Andy"

subset(Data1,Name=="Andy*")

AndyBullxxx  12
AndyPamxvb   56
AndyPantert  45

So basically a wild card symbol that will let me subset all rows that begin with a certain character designation.

r
Edit
Report

1 Answer

3

I think you want to go for a regular expression:

subset(Data1, grepl("^\bAndy", Name))

or

Data1[grepl("^\bAndy", Data1$Name),]

In the regular expression the "^" stands for startswith, and \b for the next set of characters is going to be a word. Regular expressions are a powerful text processing tool that require some study. There are a lot of tutorials and websites online. One of them that I use is:

http://www.regular-expressions.info/

I can't use R right now (my wifes laptop ;)), so these pieces of code go untested. Maybe next time provide us with an example dataset, that makes it really easy to provide a working example.

answered 2011-11-17T21:41:37.577

Your Answer