Reputation: 3941
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.
Upvotes: 17
Views: 96683
Reputation: 55715
If you are not comfortable with regex
there is a function in the utils
package, which can convert wildcard based expressions to regex. So you can do
df[grepl(glob2rx('Andy*'), rownames(df)),]
Upvotes: 13
Reputation: 28274
Try,
df[grep("^Andy",rownames(df)),]
the first argument of grep
is a pattern, a regular expression. grep
gives back the rows that contain the pattern given in this first argument. the ^ means beginning with.
Let's make this reproducible:
df <- data.frame(x=c(12,14,56,34,45,45))
rownames(df) <- c("AndyBullxxx", "AlexPullxcx","AndyPamxvb", "RickRalldfg","AndyPantert","SamSaltedf")
## see what grep does
grep("^Andy",rownames(df))
Upvotes: 25
Reputation: 60964
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.
Upvotes: 3