Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R setdiff function on vector of dates leads to strange results

Tags:

date

r

vector

I'm trying to get a vector of all the working days between to dates with the following code:

days_of_month = seq(as.Date("2017-01-01"), as.Date("2017-01-31"), by="days")
sundays = c(as.Date("2017-01-01"), as.Date("2017-01-08"), as.Date("2017-01-15"), as.Date("2017-01-22"), as.Date("2017-01-29"))

When I do:

working_days = setdiff(days_of_month, sundays)

The return value of setdiff is a vector of strange values:

[1] 17168 17169 17170 17171 17172 17173 17175 17176 17177 17178 17179 17180
[13] 17182 17183 17184 17185 17186 17187 17189 17190 17191 17192 17193 17194
[25] 17196 17197

What are those values? And how I get a vectors of the days that are in days_of_month but not in sundays?

like image 917
diegogb Avatar asked Jan 13 '17 09:01

diegogb


1 Answers

Those are internal numeric value of R S3 class Date. You can see the numeric value by as.numeric(days_of_month). Or, you can convert the result to Date by as.Date(working_days, origin="1970-01-01").

like image 167
xosp7tom Avatar answered Nov 06 '22 08:11

xosp7tom