'extract values based on first 4 integers

Suppose, I have this dataframe (integers).

df <- data.frame(date = c(20010101:20010103, 20050304, 20050506, 20220113, 20220216))

And I have the target date like this:

target_date = 2001

I want to extract only those values from df of which first 4 integers match with the target date. Result like this:

20010101 20010102 20010103


Solution 1:[1]

df[floor(df$date/10000) == target_date,]

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 Sweepy Dodo