count number of digits in a string in r

后端 未结 2 1781
温柔的废话
温柔的废话 2020-12-20 15:56

I have a column with strings:

name
aldrinas63_rios200_2001
sa_c.fr.1234

I want to count the number of digits in each cell: I have used the

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-20 16:52

    Try this:

    nchar(gsub("\\D", "", data$name))
    

    Example

    s <- c("aldrinas63_rios200_2001","sa_c.fr.1234")
    
    nchar(gsub("\\D", "", s))
    #[1] 9 4
    

提交回复
热议问题