R:将 POSIXct 类分配给数据框

发布于 2024-10-08 18:34:10 字数 279 浏览 0 评论 0原文

当我将 POSIXct 对象分配给数据帧时,它会将其转换为等效的整数,例如:

> x<-as.data.frame(matrix(nrow=1,ncol=2))
> x
  V1 V2
1 NA NA
> x[1,1]<-as.POSIXct("2010-12-07 08:00:00")
> x
          V1 V2
1 1291708800 NA

有没有办法阻止这种行为,或者在完成所有分配后轻松将整数转换回 POSIXct?

When I assign a POSIXct object into a data frame, it converts it into the integer equivalent, for example:

> x<-as.data.frame(matrix(nrow=1,ncol=2))
> x
  V1 V2
1 NA NA
> x[1,1]<-as.POSIXct("2010-12-07 08:00:00")
> x
          V1 V2
1 1291708800 NA

Is there a way to stop this behaviour, or to easily convert the integers back into POSIXct once I have done all the assignments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨小墨 2024-10-15 18:34:10

创建列后需要对其进行转换。

x <- as.data.frame(matrix(nrow=1,ncol=2))
class(x[1,1])
[1] "logical"

查看如何为它分配一个。矩阵只能有一种数据类型。

x[,1] <- as.POSIXct(x[,1])
x[1,1] <- as.POSIXct("2010-12-07 08:00:00")
class(x[1,1])
[1] "POSIXt"  "POSIXct"
x
                   V1 V2
1 2010-12-07 08:00:00 NA

当然,我不清楚为什么你一开始就把它创建为一个空矩阵。您可以轻松完成:

x <- data.frame(date=as.POSIXct("2010-12-07 08:00:00"), value=NA)

You need to convert the column after you have already created it.

x <- as.data.frame(matrix(nrow=1,ncol=2))
class(x[1,1])
[1] "logical"

See how it was already assigned a class. A matrix can only have one data type.

x[,1] <- as.POSIXct(x[,1])
x[1,1] <- as.POSIXct("2010-12-07 08:00:00")
class(x[1,1])
[1] "POSIXt"  "POSIXct"
x
                   V1 V2
1 2010-12-07 08:00:00 NA

Of course, it's unclear to me why you created it as an empty matrix to begin with. You can have just as easily have done:

x <- data.frame(date=as.POSIXct("2010-12-07 08:00:00"), value=NA)
爱你是孤单的心事 2024-10-15 18:34:10

我有完全相同的问题。为了解决这个问题,我使用 as.POSIXct() 声明了数据框的列类。

示例:

> temp = data.frame(col1 = NA)
> temp[1,] = Sys.time()
> str(temp)
'data.frame':   1 obs. of  1 variable:
 $ col1: num 1.4e+09

> temp = data.frame(col1 = as.POSIXct(NA,""))
> temp[1,] = Sys.time()
> str(temp)
'data.frame':   1 obs. of  1 variable:
 $ col1: POSIXct, format: "2014-05-21 15:35:46"

有趣的是,即使初始默认列类是“逻辑”:

> temp = data.frame(col1 = NA)
> str(temp)
'data.frame':   1 obs. of  1 variable:
 $ col1: logi NA

您可以向其中写入字符:

> temp = data.frame(col1 = NA)
> temp[1,] = "hello"
> str(temp)
'data.frame':   1 obs. of  1 variable:
 $ col1: chr "hello"

但是,像 POSIXct 一样,您不能写入因子:

> temp = data.frame(col1 = NA)
> temp[1,] = as.factor("hello")
> str(temp)
'data.frame':   1 obs. of  1 variable:
 $ col1: int 1

I had exactly the same problem. To fix this, I declared the column class of the data frame using as.POSIXct().

Example:

> temp = data.frame(col1 = NA)
> temp[1,] = Sys.time()
> str(temp)
'data.frame':   1 obs. of  1 variable:
 $ col1: num 1.4e+09

But

> temp = data.frame(col1 = as.POSIXct(NA,""))
> temp[1,] = Sys.time()
> str(temp)
'data.frame':   1 obs. of  1 variable:
 $ col1: POSIXct, format: "2014-05-21 15:35:46"

Interestingly, even though the initial default column class is "logical":

> temp = data.frame(col1 = NA)
> str(temp)
'data.frame':   1 obs. of  1 variable:
 $ col1: logi NA

You can write characters to it:

> temp = data.frame(col1 = NA)
> temp[1,] = "hello"
> str(temp)
'data.frame':   1 obs. of  1 variable:
 $ col1: chr "hello"

However, like POSIXct, you cannot write factors:

> temp = data.frame(col1 = NA)
> temp[1,] = as.factor("hello")
> str(temp)
'data.frame':   1 obs. of  1 variable:
 $ col1: int 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文