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

R: delete rows that have two columns with the same first digit

I want to delete rows whose county fips codes start with the same number.

# What I have ----

x.dist = data.frame(county1=c(8001,8001,8001),
                    mi_to=c(10:12),
                    county2=c(8005,34502,8007))
> x.dist
  county1 mi_to county2
1    8001    10    8005
2    8001    11   34502
3    8001    12    8007

# What I want ----

  county1 mi_to county2
2    8001    11   34502

In the original df, county1 and county2 both start with 8 in rows 1 and 3. I want the first digit in each column to differ so I’m left with row 2. How do I do this?

I tried this, but absolutely nothing happened.

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

w.dist = x.dist %>%   
filter(str_sub(county1, start= 1) != (str_sub(county2, start= 1)))

>Solution :

You are using str_sub() with the default end parameter, which takes you to the end of the string:. Try this:

x.dist %>% filter(str_sub(county1,1,1)!=str_sub(county2,1,1))
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