r - 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
解决方案
你可以试试:
tapply(df$count, gsub("^\\d.*|[A-Za-z]", "", df$string), sum)
@123 123 223
65 21 22 3
推荐阅读
- rcpp - 错误:依赖项“RcppArmadillo”不可用于包
- php - 使用 wp_nav_menu_disabled_check 将项目添加到 Wordpress 菜单
- facebook - Facebook 自定义转化
- r - R,基于另一个属性从一个属性中提取值
- reactjs - 材质 UI - 卡片内容未扩散到填充区域
- python - 使用 ODE 系统中的变化参数模拟一个 diff 方程
- python - 基于多个条件的带有条件彩色条的条形图
- html - [EJS]:MIME 类型 ('text/html') 不是受支持的样式表 MIME 类型
- performance - 当添加或动画图像时,为什么浏览器不绘制画布?
- git - 提交文件转到上一次提交