Does R have function startswith or endswith like python? [closed]
As added to base
in 3.3.0, startsWith
(and endsWith
) are exactly this.
> startsWith("what", "wha")[1] TRUE> startsWith("what", "ha")[1] FALSE
https://stat.ethz.ch/R-manual/R-devel/library/base/html/startsWith.html
Not inbuilt like that.
Options include grepl
and substr
.
x <- 'ABCDE'grepl('^AB', x) # starts with AB?grepl('DE$', x) # ends with DE?substr(x, 1, 2) == 'AB'substr('ABCDE', nchar(x)-1, nchar(x)) == 'DE'
The dplyr package's select
statement supports starts_with
and ends_with
. For example, this selects the columns of the iris data frame that start with Petal
library(dplyr)select(iris, starts_with("Petal"))
select
supports other subcommands too. Try ?select
.