0

I have a dataframe that looks like this:

> data
Simple feature collection with 176 features and 2 fields
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: 4358985 ymin: 2571551 xmax: 4505751 ymax: 2663100
projected CRS:  Lambert_Azimuthal_Equal_Area
First 10 features:
    id  slide_dates                       geometry
1   35        12976 POLYGON ((4481002 2663025, ...
2   62        10277 POLYGON ((4472778 2661600, ...
3   82        11400 POLYGON ((4477953 2660650, ...
4   95         8520 POLYGON ((4476978 2658650, ...
5   97        14115 POLYGON ((4474878 2658775, ...
6  166        12017 POLYGON ((4445105 2656175, ...
7  198        10273 POLYGON ((4472753 2655225, ...
8  351         7073 POLYGON ((4467303 2651675, ...
9  377 10149, 16092 POLYGON ((4464129 2652250, ...
10 426        13685 POLYGON ((4432231 2650525, ...

The column slide_dates is a list with dates stores as integer days after 1970. I would like to replace every element in each list with its "correct" date. I tried several approaches, like this one:

data %>% 
    mutate(slide_dates = as.Date.numeric(slide_dates, origin="1970-01-01"))

but I get the following error

Fehler: Problem with `mutate()` input `slide_dates`.
x nicht-numerisches Argument für binären Operator
i Input `slide_dates` is `as.Date.numeric(slide_dates, origin = "1970-01-01")`

And I'm not really sure how to solve this problem. Any help is appreciated:)

Some example Data

This gives the same error

df = data.frame(a=1:2)
df$b = list(c(1), c(2, 3))
df %>% mutate(b = as.Date.numeric(b, origin=Sys.Date()))
Robin Kohrs
  • 655
  • 7
  • 17
  • 1
    Why are you using `sapply` in mutate? `as.Date.numeric` is vectorized. Also, could you add a [minimale reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)?. The MRE will make it easier for others to find and test a answer to your question. – dario Feb 10 '21 at 15:41
  • Thanks a lot, I update it a very little bit. Just two lines, but the problem is the same I think:) – Robin Kohrs Feb 10 '21 at 16:06
  • How does the example data you provided give you the same (x binary + is not defined for "Date" objects) error?? In your example data the thing we want to cast to a date is a list, therefore we get a " non-numeric argument to binary operator" error?! Please make sure your MRE applies to the question... – dario Feb 10 '21 at 16:07
  • You are right, it did not. Now they both give the same error. I was not aware of the fact that `as.Date.numeric` is already vectorized and updated the code in the first place – Robin Kohrs Feb 10 '21 at 16:12
  • 1
    I think the main problem is that "slide_dates" is a list of lists of dates (and not simply a list of dates). This leads to multiple problems when casting to a data.frame and trying to use the "normal" dplyr syntax.... combine that with the fact that `sapply` doesn't play well with date types... oh well – dario Feb 10 '21 at 17:07
  • haha thanks a lot!! This is what I sometimes don't really like about R for me. There are so many ways that can super confuse a beginner... Anyways, thanks a lot:) I got around it in another way – Robin Kohrs Feb 10 '21 at 17:18

1 Answers1

1

So first challenge, I'm not sure how I'm supposed to reproduce this...

That being said, problem doesn't seem to require sapply, you're defacto changing a row? Minus your variables,

data <- data.frame(slide_dates = c(12976,10277,11400,8520,14115,12017, 10273,7073,16092,13685)) %>%
     mutate(slide_dates = as.Date(slide_dates, origin = "1970-01-01")) 

Or you could do it without mutate as

data$slide_dates <- as.Date(data$slide_dates, origin = "1970-01-01"))