0

Hei! :-) I have downloaded a dataset where I want annual data. However, the cells also include the date. So it is in a format like this:

1968-12-01
1968-12-02
(...)
2022-12-01

However, I want it to be like this:

1968
1969
(...)
2022

I tried to filter out the cells that do not include 12-01, because then I thought I will only get one observation each year. However, I think my code meant that 12-01 is the only thing that should be in the cell, not for example 2000 - 12-01. Does anyone know about a way to filter out the dates, and only get one observation per year? Or is that not possible? Best regards Mari

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • 4
    Welcome to stackoverflow. What we need is a question (already done), a minimal example data frame (is lacking, you can do it with dput(df) for example), and the addition of the desired output would be great. This is important in two ways: First, you will increase your learning curve dramatically by doing so and second, you will make us happy to be able to help! See – TarJae Jan 18 '23 at 17:08
  • `lubridate::year(as.Date("1968-12-01"))` should work, by converting the dates to Date format (if they aren't already -- you don't specify), and then extracting the year part from those dates. So `lubridate::year(as.Date(YOUR_VECTOR_OF_DATES))` should work to extract the years. How do you want to pick which one observation to keep per year? – Jon Spring Jan 18 '23 at 17:24
  • @JonSpring Thank you a lot! This worked out as it gave me all the years in the output. Do you know if it is possible to replace it into the dataset also? So that I have only the year left in the dataset? (and which observation doesnt matter I think, as long as I have only one each year, an preferable measured on the same date each year). Best regards from Mari – mari tonnessen Jan 18 '23 at 18:55
  • And also thank you for editing my post,- I have not used R a lot and not stackoverflow either, so I was not really sure how to put it in the right format. – mari tonnessen Jan 18 '23 at 18:56
  • Oh, now I managed to make a new column! :-) So reducing to only one measurment every year is the only step i lack – mari tonnessen Jan 18 '23 at 19:19

0 Answers0