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

Time:04-24

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.

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

CodePudding user response:

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))
  • Related