Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I remove the values of a column of a data set that contain specific number of digits in R using dplyr?

I have a data set that looks like this :

vec = c(989, 987, 145, 315, 8449, 9999999999000)
char = c("a","b","c","d","e","f")
df2 = tibble(vec,char);df2
# A tibble: 6 × 2
            vec char 
          <dbl> <chr>
1           989 a    
2           987 b    
3           145 c    
4           315 d    
5          8449 e    
6 9999999999000 f    

I want to remove the values from the column vector that contains more than or equal to 5 digits. Ideally I want to look like this :

1           989 a    
2           987 b    
3           145 c    
4           315 d    
5          8449 e    

How can I do this in R using dplyr ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help ?

>Solution :

Use nchar in base R

subset(df2, nchar(vec) <6)

Or filter

library(dplyr)
filter(df2, nchar(vec) <6)
# A tibble: 5 × 2
    vec char 
  <dbl> <chr>
1   989 a    
2   987 b    
3   145 c    
4   315 d    
5  8449 e    

If there are decimals, convert to integer and count

filter(df2, nchar(as.integer(vec)) < 6)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading