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

后端 未结 4 1928
面向向阳花
面向向阳花 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条回答
  •  忘掉有多难
    2021-01-13 10:21

    A couple of observations:

    Cnverting a string to uppercase can be done with toupper, e.g.:

    > toupper('be33szfuhm100060')
    > [1] "BE33SZFUHM100060"
    

    You could use substr to extract a substring by character positions and paste to concatenate strings:

    > x <- 'be33szfuhm100060'
    > paste(substr(x, 1, 2), substr(x, 5, nchar(x)), sep='')
    [1] "beszfuhm100060"
    

提交回复
热议问题