简体   繁体   中英

Reverse subsetting a vector in R

I'm trying to reverse subset code in R. Here is an example of my code below. Instead of returning the 20 characters beyond the comma, i want to return the 20 characters prior to the comma. Thanks in advance.

substr(test,gregexpr(pattern =',',test),20)
# Sample "text"
text = "asdasdasdasd,asdasdasdasdasd"
index = gregexpr(pattern =',',text)[[1]]
substr(text,(index-20), index)

> substr(text,(index-20), index)
[1] "asdasdasdasd,"

substr will return up till the beginning if there are less than 20 characters from ",".

> substr(text,(index-5), index)
[1] "sdasd,"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM