简体   繁体   中英

Replacing elements within a string in R

I have a row in a data frame in R that is made up of sequences of the undetermined length of 0s 1s and 2s as characters. So "01", "010", "201", "102", "00012"... things like this.

I'd like to find a way to determine if the last character in the string is NUMERICALLY the largest. It's important that I keep the row in the data frame as characters for other purposes. So basically I want to take substr(x, nchar(x), nchar(x)) and determine if it, as a number, is the largest of the numbers in the character string.

I'm super lost as to how to do this since I'm not all that familiar with regular expressions and I have to back and forth between treating elements like characters and numbers.

Thanks in advance.

~Maureen

Let df be the name of the dataframe and the row with the string sequences "01", "010", "201", "102", "00012" is No.2. You can get a vector that answers the question if the last character in the string is NUMERICALLY the largest giving this:

sapply(strsplit(as.character(df[2,]),""),function(x) x[length(x)] >= max(x))
[1]  TRUE FALSE FALSE  TRUE TRUE

One way would be

p <- as.numeric(strsplit("0120102","")[[1]])
if (max(p) == p[length(p)]) {
   print("yes")
}

Actually you can ignore as.numeric() since "2" > "1" > "0":

p <- strsplit("0120102", "")[[1]]

If you wanted to apply this to your data.frame A:

apply(A, c(1,2), function(z) {p<-strsplit(z, "")[[1]];(max(p) == p[length(p)])})

正则表达式为[0-9] $以获得最后一个数字,其余逻辑取决于您所开发的环境。

I think you're best bet will be to look at how regex works in the R language:

http://www.regular-expressions.info/rlanguage.html

Like Dan Heberden said in the above post, you'll need to tokenize the string you gave as an example in your post, and then grep( ...? ) the tokens for the regex "[0-9]$". By the way, with regex, you can treat everything as characters, so you shouldn't have to shuttle back and forth between numeric and character mode, except for when you take the results of the grep function and parse it to numeric form for your comparison.

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