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

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

Leave a Comment