I need to join two columns of DATE and TIME with the class Posixct but returns me NA

Asked

Viewed 58 times

-1

I need to create a Datetime by joining these two columns with the class Posixct. But when returning the table the Datetime comes with NA.

Edt. Following the suggestion of the friend below only appeared the three initial columns. My table has more columns. I had put here only three to exemplify not to get too big here. (My base sheet has more columns).

Below is a sample of data for reproduction:

 Mycbon
Date        Time      Receiver  Transmitter ID  Transmitter.Serial  Sensor Value    
2019-04-29  05:31:33    134321  4844    Ane  1305232           28.7  °C     PRN
2019-04-29  08:52:08    134325  4861    Bob  1305233           4.2    m     MVW



    class(mycbon$Date)
    class(mycbon$Time)
    mycbon$DateTime <- strptime(paste(mycbon$Date, mycbon$Time), %Y-%m-%d %H:%M:OS")
    mycbon$DateTime<-as.character(mycbon$DateTime)
    mycbon$DateTime<-as.POSIXct(strptime(as.character(mycbon$DateTime),"%Y-%m-%d %H:%M:%OS"))
    str(mycbon)
    mycbon[,3:12]->myc
    myc <- myc[c("DateTime","Receiver", "Transmitter","ID", "Transmitter.Serial", "Sensor", "Value")]
    head(myc)
  • Please make a reproducible example and a better structured question.

  • What is the class of Date and Time columns? Provide a better example of your data, for example by posting the output of dput(head(mycbon)).

1 answer

0

Nas are generated because of the extra spaces when passing the format to strptime.

mycbon <- read.table(text = "
  Date         Time      Receiver Transmitter
  29/04/2019  05:31:33    134156  4822
  29/04/2019  08:52:08    134158  4823",
  header = TRUE)

DateTime <- as.POSIXct(paste(mycbon$Date, mycbon$Time),
                       format = "%d/%m/%Y %H:%M:%S")

> cbind(DateTime, mycbon[, 3:ncol(mycbon)])
             DateTime Receiver Transmitter
1 2019-04-29 05:31:33   134156        4822
2 2019-04-29 08:52:08   134158        4823

NOTE: as.Posixct already internally makes the call to strptime, no need to use as.POSIXct(strptime(...))

  • I removed the spaces from the format and deleted the line from the `as.Posixct but still resulting in NA ``` class(mycbon$Date)&#xA;class(mycbon$Time)&#xA;mycbon$DateTime <- strptime(paste(mycbon$Date, mycbon$Time), "%Y/%m/%d% H:%M:%OS")&#xA;mycbon$DateTime<-as.character(mycbon$DateTime) str(mycbon) mycbon[,3:12]->Myc Myc <- Myc[c("Datetime","Receiver", "Transmitter")] head(Myc)

  • How do I show up here in the answer the commands in gray separated from the text? I answered but it was all together. Obg

  • It does not, the comment session is not for that.

  • See the format you are passing to strptime: it should match your data, not the format you want at the end.

  • I already corrected, but continues the Nas. I updated the information above. I hope you can help me to solve this problem. Obg.

  • Pay attention to your code, it’s just a typo.

  • It worked! Thank you!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.