Does R Have a Wildcard Expression (Such as an Asterisk (*))?
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...
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.
3 Answers
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))
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)),]
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:
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.