Does R Have Function Startswith or Endswith Like Python

Does R have function startswith or endswith like python?

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'

ifelse starting with, ends with, includes

You can use grepl() (see help(grepl) and help(regex)):

ifelse(grepl('^T', df$Name), 'YES', 'NO')
ifelse(grepl('i', df$Name), 'YES', 'NO')
ifelse(grepl('s$', df$Name), 'YES', 'NO')

which results in the following output (easily checkable):

> ifelse(grepl('^T', df$Name), 'YES', 'NO')
[1] "YES" "NO" "YES" "NO"
> ifelse(grepl('i', df$Name), 'YES', 'NO')
[1] "NO" "NO" "YES" "YES"
> ifelse(grepl('s$', df$Name), 'YES', 'NO')
[1] "NO" "NO" "NO" "YES"

Details

grepl() returns a logical vector the same length as the vector that is the function's second argument, returning TRUE where the regular expression of the function's first argument is present and FALSE for elements where the expression is not found.

In regular expressions, typically, and in R in particular, ^ matches the first character and $ matches the last. So, ^T is a regular expression looking for a string that begins with T while s$ is a regular expression looking for a string that ends in s.

If condition with startswith and endswith error

Hope you are trying to split by (chars) or ,,

>>> s = '(1)Basking Ridge, NJ (33)Knoxville, TN'
>>> import re
>>> re.split(r'\s*\([^()]*\)\s*|\s*,\s*', s)
['', 'Basking Ridge', 'NJ', 'Knoxville', 'TN']
>>> t = re.split(r'\s*\([^()]*\)\s*|\s*,\s*', s)
>>> ','.join([i for i in t if i])
'Basking Ridge,NJ,Knoxville,TN'
>>>

How to get startsWith function to return values, not Boolean

We can use grep with value = TRUE (by default, it return the numeric index or position index)

grep("^factor\\(state\\)", string, value = TRUE)

If we have a logical vector, we can use that as index to subset

string[startsWith(string , "factor(state)")]

How to print portion of the string from 'startswith' till 'endswith'

At the end of each line there is a character to tell the computer to show a new row. I am assuming here that "start with string" and "ends with string" are on the same line. If this is not the case add --"id.append(ln[:])"-- directly below the first if statement.

Try

ln.endswith("ends with string"+'\n' )

or

ln.endswith("ends with string"+'\n' +'\r')
with open('C:\\Py\\testing.txt','r') as fi:
id = []
x = 0
copy_line = False
for ln in fi:
if "starts with string" in ln:
copy_line = True
if copy_line:
id.append ( ln[:] )
if "ends with string" in ln :
copy_line = False

with open ('C:\\Py\\testing_out.txt', 'a', encoding='utf-8' ) as fo:
fo.write (",".join(id))

print(id)

Checking whether a string starts with XXXX

aString = "hello world"
aString.startswith("hello")

More info about startswith.

Shortest code for printing line startswith ['a', 'b'] and endswith ['y', 'z'] Python 3

You could do this:

line = line.rstrip()
if line and line[0] in "WZ" and line[-1] in "nt":
print(line)

Or use regular expressions:

import re 
# ...
if re.match(r'^[WZ].*[nt]$', line):
print(line)

# ^ beginning of string
# $ end of string
# [WX] matches W or X
# .* any character 0 or more times

See the docs on Python regex syntax.

String startswith a particular text

In addition to the above concise answers, if you want to use a similar if else structure, you can use set_value to set the value of a specific row and column.

import pandas as pd
inf = pd.read_csv('string.csv')
for index,row in inf.iterrows():
if "RT @" in row["Tweet"]:
inf.set_value(index, "Engagements", 0)
else:
inf.set_value(index, "Engagements", row["Favorite_Count"] + row["Retweet_Count"])
inf.to_csv('string2.csv', index=False)

Finding all elements of a string vector that end with a given string in R

Here is one method using grep:

grep("##$", test)

This returns indices 1 and 3 as a vector. The "##$" is a regular expression that says match if ## are the last two characters, the "$". anchors the ## to the end.



Related Topics



Leave a reply



Submit