Quite new to R so this may be an easy answer. I have a list of characters. I want to remove the last letter and iterate it by one so A becomes B, 1 becomes 2 etc.
waferlist<-c('L2MLQ','L2MIW','L2MK0','L2ML6','L2MO2','L2MHE','L2MK4','L2MN6','L2MLM')
for (i in waferlist)
{
lastchar<-substr(i,5,6) #Get last character
k<-lastchar==LETTERS #Is it a Letter
pos<-min(which(k==TRUE)) #Find letter position and itterate
pos<-pos+1
pos<-LETTERS[pos]
The problem I'm having is if the last character is a number, it returns it as an Inf or NA_character_ as its not in LETTERS.
I've tried to find a way to select these non results below but it doesn't see it as a TRUE/FALSE statement so it doesn't work. Is there another way to do this?
if(pos==Inf | pos==NA_character_)
{
lastchar<-as.numeric(lastchar)
pos<-lastchar+1
}
For an efficient solution (assuming you are using capitals),
res <- sapply(waferlist, function(i) {
out <- utf8ToInt(i)
out[[nchar(i)]] <- out[[nchar(i)]] + 1
if (out[[nchar(i)]] == 91) out[[nchar(i)]] <- 65
## For 9 cycling back to 0?
else if (out[[nchar(i)]] == 58) out[[nchar(i)]] <- 48
intToUtf8(out)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With