Extract number from a column with text & number

I have a data frame column that looks like this:

CHR
Chr1
Chr14
Chr19
Chr2
Chr8
Chr7

I want to create a new column which looks as follows:

CHR_N
1
14
19
2
8
7

There are several ways to do this. The following example uses the sub() function from base R. I used the as.numeric() function to convert the result of the sub() function into a number. If you want the result to be a character, you can remove that.

DF <- data.frame(CHR=c("Chr1", "Chr14", "Chr19", "Chr2", "Chr8", "Chr7"))
DF
#>     CHR
#> 1  Chr1
#> 2 Chr14
#> 3 Chr19
#> 4  Chr2
#> 5  Chr8
#> 6  Chr7

DF$CHR_N  <- as.numeric(sub(pattern = "Chr",replacement = "", DF$CHR))
DF
#>     CHR CHR_N
#> 1  Chr1     1
#> 2 Chr14    14
#> 3 Chr19    19
#> 4  Chr2     2
#> 5  Chr8     8
#> 6  Chr7     7

Created on 2021-10-13 by the reprex package (v2.0.1)

Both of these codes, output the result in the console. How can I make changes in the data frame itself? Thank you

I see a new column with all NAs and following warning:

Warning message:
NAs introduced by coercion

The code I posted is self contained and, as you can see, works for me. Do the two lines that actually matter

DF <- data.frame(CHR=c("Chr1", "Chr14", "Chr19", "Chr2", "Chr8", "Chr7"))
DF$CHR_N  <- as.numeric(sub(pattern = "Chr",replacement = "", DF$CHR))

work for you? If so please post a small example of your data where similar code is not working. If your data frame is named DF, you can post the output of

dput(head(DF))

Please put lines with only three back ticks before and after the output, like this
```
your output here
```

1 Like

Thank you very much! I was able to solve it using this:

OLD$num <- readr::parse_number(OLD$CHR)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.