as.posixct when applied for an element in the data frame returns a number instead of date and time

无人久伴 提交于 2019-12-11 22:57:44

问题


Here is the existing data:

I have 2 columns of data. Each row of the first column has data whereas only certain rows of the second column has data (others being blank). I want to convert the format of the data with the help of as.POSIXct(). For the first column I used the following code (I named the data frame as 'mrkt'):

mrkt[1]<-lapply(mrkt[1],as.POSIXct)

This worked well in terms of converting the existing data to the right format For the second column the above code won't work as the as.POSIXct() cannot address "" values. So I wrote a loop instead:

for (i in 1:dim(mrkt[2])[1]){
   if (!as.character(mrkt[[2]][i])==""){
       mrkt$open_time[i]<-as.POSIXct(mrkt$open_time[i])
    }
 }

However this is giving me weird outputs in the form of a number. How can I avoid that? Here is the output:


回答1:


An easy way to do this would be to do this:

library(plyr)
library(dplyr)

mrkt %>%
  mutate(send_time = send_time %>%
                       as.POSIXct,
         open_time = open_time %>%
                       mapvalues("", NA) %>%
                       as.POSIXct)



回答2:


This is due to implicit typecasting from POSIXct to numeric. This only happens in the loop because the vector has an assigned type and values are casted to this type if single values are assigned. When the whole vector is replaced a new vector is created with the right type.

The simplest solution is to use as.POSIXct(strptime(mrkt$open_time, format=yourformat)), with a correctly defined format, see ?strptime for the formats. This is vectorized, and strptime handles empty Strings correctly (returning NA).



来源:https://stackoverflow.com/questions/33511213/as-posixct-when-applied-for-an-element-in-the-data-frame-returns-a-number-instea

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!