简体   繁体   中英

Extracting numeric portion from a character in the Data Frame

I am trying to extract the numeric portion of this string from DF$Numbers like changing W12K32 to 1232

Current DF

  Name Numbers
1 Alex  W12K32
2  Tom S12WE23
3 Eric   T1243

Desired Output

  Name Numbers
1 Alex    1232
2  Tom    1223
3 Eric    1243

Use sub and strip off all non numeric characters:

df$Numbers <- gsub("\\D+", "", df$Numbers)
df

  Name Numbers
1 Alex    1232
2  Tom    1223
3 Eric    1243

Data:

df <- data.frame(Name=c("Alex", "Tom", "Eric"), Numbers=c("W12K32", "S12WE23", "T1243"),
                 stringsAsFactors=FALSE)

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