Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 212038

Why do I lose certain dates after datetime conversion?

$
0
0

Edit, previous question below: by changing long data format (from "%d %b %Y %H:%M:%S" to "%Y-%m-%d") certain months get lost in NAs (May, October and December).

Here is an reproducible example, where May and December become NA:

Time <- c("26 Apr 2019 12:14:05", "17 May 2019 12:07:31", "05 Jul 2019 07:43:51", "14 Dec 2019 21:48:47")

a <- c(1, 2, 3, 4)
b <- c(2, 4, 6, 1)
c <- c(3, 5, 9, 7)
d <- c(4, 3, 2, 1)

my.df <- data.frame(Time, A = a, B = b, C = c, D = d)

my.df$Time <- format(as.POSIXct(my.df$Time,"%d %b %Y %H:%M:%S",tz=""), format = "%Y-%m-%d")

my.df

        Time A B C D
1 2019-04-26 1 2 3 4
2       <NA> 2 4 5 3
3 2019-07-05 3 6 9 2
4       <NA> 4 1 7 1

Thanks for your help!

================== PREVIOUS QUESTION ==================

I am refering to an earlier question I already "solved": How can I simplify my date-time stamp in R?

So here is my data frame:

df <- read.table("Diviner 2019-12 (Pro26).csv", skip = 9, dec = ".", sep = ",", stringsAsFactors = F)
names(df) <- df[1,]
df <- df[-c(1),]
head(df)
                  Time       10cm       20cm       30cm       40cm      50cm      60cm      70cm      80cm
2 05 Apr 2019 09:46:13  20.706751  26.204191  23.663700  18.041510  3.507654  5.644918  3.947458  0.926415
3 11 Apr 2019 08:36:32  18.457157  25.762735  23.822021  18.596792  3.829793  6.639636  4.313009  1.002555
4 19 Apr 2019 09:24:16  17.224855  24.033939  21.703968  16.956991  3.507654  6.827912  4.417910  1.046471
5 26 Apr 2019 12:14:05  16.603245  22.160437  19.541498  15.735872  3.237127  6.169124  3.987147  1.002555
6 10 May 2019 07:40:20  19.685282  22.467394  19.208130  14.976073  3.184556  5.620616  3.888364  0.959796
7 17 May 2019 12:07:31  16.823887  23.139757  20.706751  16.868202  3.470846  5.500014  3.714201  0.985313

Data ranges from the beginning of April to the end of December. After converting I lose the months May, October and December without any obvious reason:

df$Time <- format(as.POSIXct(df$Time,"%d %b %Y %H:%M:%S",tz="UTC"), format = "%Y-%m-%d")
head(df)
        Time       10cm       20cm       30cm       40cm      50cm      60cm      70cm      80cm      90cm
2 2019-04-05  20.706751  26.204191  23.663700  18.041510  3.507654  5.644918  3.947458  0.926415  1.304021
3 2019-04-11  18.457157  25.762735  23.822021  18.596792  3.829793  6.639636  4.313009  1.002555  1.440603
4 2019-04-19  17.224855  24.033939  21.703968  16.956991  3.507654  6.827912  4.417910  1.046471  1.574125
5 2019-04-26  16.603245  22.160437  19.541498  15.735872  3.237127  6.169124  3.987147  1.002555  1.397690
6       <NA>  19.685282  22.467394  19.208130  14.976073  3.184556  5.620616  3.888364  0.959796  1.484311
7       <NA>  16.823887  23.139757  20.706751  16.868202  3.470846  5.500014  3.714201  0.985313  1.429803

Why does it work on all months but not on May, October and December?


Viewing all articles
Browse latest Browse all 212038

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>