'How to delete text that does not start with a certain amount of numbers in dataframe column

I have this:

col1
1234HO
9535KU
4532SP
1
hello
xyz
1206
9530OK
23
8524US

And I need it to be this:

col1     col2    col3
1234HO   1234    HO
9535KU   9535    KU
4532SP   4532    SP
                       #these rows still need to be there


1206     1206          #keep in mind that I still want to keep this if there is 4 numbers
9530OK   9530    OK

8524US   8524    US

I tried removing it manually, but it's a bit too much work. I am not sure how to make a function that says "delete all text that does not start with 4 numbers". I would only know how to do it if they were all the same numbers, but they can be any numbers.



Solution 1:[1]

You can use tidyr::separate and then filter.

library(dplyr)
library(tidyr)

dat %>%
  separate(col1, into = c("num", "text"), sep = "(?<=[0-9])(?=[A-Za-z])", remove = F) %>% 
  filter(!grepl("[A-Za-z]", num) & nchar(num) > 3)

    col1  num text
1 1234HO 1234   HO
2 9535KU 9535   KU
3 4532SP 4532   SP
4   1206 1206 <NA>
5 9530OK 9530   OK
6 8524US 8524   US

Solution 2:[2]

Another possible solution:

library(tidyverse)

df <- data.frame(
  stringsAsFactors = FALSE,
  col1 = c("1234HO","9535KU",
           "4532SP","1","hello","xyz","1206","9530OK","23",
           "8524US")
)

df %>% 
  separate(col1, into=str_c("col", 2:3), sep="(?<=\\d{4})",
     remove = F, fill = "right") %>% filter(!is.na(col3))

#>     col1 col2 col3
#> 1 1234HO 1234   HO
#> 2 9535KU 9535   KU
#> 3 4532SP 4532   SP
#> 4   1206 1206     
#> 5 9530OK 9530   OK
#> 6 8524US 8524   US

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 PaulS