首页 > 解决方案 > Finding the trailing numbers in R

问题描述

I have a data frame with 2 columns

<string> <count>

for example:

qwerty 24
1qwerty 21
123456 20
qwerty123 12
abc123 10
xyz223 1
test223 2
test@123 11
xyz@123 10

I want to make a data frame with the structure

<suffix> <count>

suffix will contain the trailing numbers or a symbol followed by a number. The suffix of any string that contains only numbers will be NA (in this example "qwerty", "123456", and "1qwerty" would be NA)

count will be the sum of all the counts in the first data frame that has that type of suffix

ie. the required output for the example would be

NA 65
123 22
@123 21
223 3

标签: rregextrailing

解决方案


你可以试试:

tapply(df$count, gsub("^\\d.*|[A-Za-z]", "", df$string), sum)

     @123  123  223 
  65   21   22    3 

推荐阅读