R: How can I replace let's say the 5th element within a string?

后端 未结 4 1886
面向向阳花
面向向阳花 2021-01-13 09:32

I would like to convert the a string like be33szfuhm100060 into BESZFUHM0060.

In order to replace the small letters with capital letters I\'ve so far used the gsub f

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 10:13

    As an alternative, if you are going to be doing this alot:

    String <- function(x="") {
      x <- as.character(paste(x, collapse=""))
      class(x) <- c("String","character")
      return(x)
    }
    
    "[.String" <- function(x,i,j,...,drop=TRUE) {
      unlist(strsplit(x,""))[i]
    }
    "[<-.String" <- function(x,i,j,...,value) {
      tmp <- x[]
      tmp[i] <- String(value)
      x <- String(tmp)
      x
    }
    print.String <- function(x, ...) cat(x, "\n")
    ## try it out
    > x <- String("be33szfuhm100060")
    > x[3:4] <- character(0)
    > x
    beszfuhm100060
    

提交回复
热议问题