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

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:

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

20010101 20010102 20010103

>Solution :

We may use substr to extract the first four character and then compare (==) with target_date to extract the subset of rows and pull the ‘date’ column

subset(df, substr(date, 1, 4) == target_date)$date
[1] 20010101 20010102 20010103

Or another option is to convert to Date class, extract the year and then filter

library(dplyr)
library(lubridate)
df %>%
  filter(year(ymd(date)) == target_date)
      date
1 20010101
2 20010102
3 20010103
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