Does R have function startswith or endswith like python? [closed]
Asked Answered
J

6

94
> startsWith('abc', 'a')
[1] TRUE
> startsWith('abc', 'c')
[1] FALSE

> endsWith('abc', 'a')
[1] FALSE  
> endsWith('abc', 'c')
[1] TRUE
Josejosee answered 17/7, 2015 at 2:55 Comment(1)
You could use a regular expression, like iris[grepl("^Petal",names(iris))]Aron
A
42

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'
Axletree answered 17/7, 2015 at 3:10 Comment(1)
(just a note) From R 3.3.0, functions startsWith and endsWith exist.Tirade
M
139

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

Mac answered 1/7, 2016 at 18:26 Comment(6)
Thanks, ijoseph, please see alexis_laz's comment under the accepted answer.Josejosee
@Chen - I think it still belongs as an answer, since it's what you were originally looking for. Comments are not set in stone.Remorseless
@Chen ah, I completely missed that comment before.Mac
It was added in 3.3.0: "New string utilities startsWith(x, prefix) and endsWith(x, suffix)." from the R changelog cran.r-project.org/doc/manuals/r-devel/NEWS.htmlOusley
Updated to add certainty. Thanks for investigation, @FrankPinter .Mac
Related: the tidyverse package makes this available - str_starts('abc', 'ab' ) # TRUEDiaphoretic
A
42

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'
Axletree answered 17/7, 2015 at 3:10 Comment(1)
(just a note) From R 3.3.0, functions startsWith and endsWith exist.Tirade
C
14

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 .

Couchant answered 17/7, 2015 at 3:14 Comment(2)
The starts_with() function is for selecting columns, not rows!Diaphoretic
@Diaphoretic , This has nothing to do with selecting rows.Couchant
D
13

The simplest way I can think of is to use the %like% operator:

library(data.table)

"foo" %like% "^f" 

evaluates as TRUE - Starting with f

"foo" %like% "o$" 

evaluates as TRUE - Ending with o

"bar" %like% "a"

evaluates as TRUE - Containing a

Drysalt answered 16/6, 2016 at 15:36 Comment(0)
D
3

This is relatively simple by using the substring function:

> strings = c("abc", "bcd", "def", "ghi", "xyzzd", "a")
> str_to_find = "de"
> substring(strings, 1, nchar(str_to_find)) == str_to_find
[1] FALSE FALSE  TRUE FALSE FALSE FALSE

You cut each string to the desired length with substring. The length being the number of characters you are looking for at the beginning of each string.

Delectable answered 17/7, 2015 at 3:11 Comment(0)
M
3

Borrowing some code from the dplyr package [see this] you could do something like this:

starts_with <- function(vars, match, ignore.case = TRUE) {
  if (ignore.case) match <- tolower(match)
  n <- nchar(match)

  if (ignore.case) vars <- tolower(vars)
  substr(vars, 1, n) == match
}

ends_with <- function(vars, match, ignore.case = TRUE) {
  if (ignore.case) match <- tolower(match)
  n <- nchar(match)

  if (ignore.case) vars <- tolower(vars)
  length <- nchar(vars)

  substr(vars, pmax(1, length - n + 1), length) == match
}
Mainspring answered 17/7, 2015 at 3:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.