用POSIXlt对象替换列值

Dan*_*iel 1 r dataframe posixct assign

考虑以下R代码,它用一组POSIXct值替换数据框的一列中的值:

foo <- as.data.frame(list(bar=rep(5,5)))
bar <- as.POSIXct(rep(5,5), origin="1970-1-1", tz="c")
foo[,1] <- bar
Run Code Online (Sandbox Code Playgroud)

我的问题:当我尝试使用POSIXlt时,为什么同样的操作失败?例如:

foo <- as.data.frame(list(bar=rep(5,5)))
bar <- as.POSIXlt(rep(5,5), origin="1970-1-1", tz="c")
foo[,1] <- bar
Warning message:
   In `[<-.data.frame`(`*tmp*`, , 1, value = list(sec = c(5, 5, 5,  :
   provided 9 variables to replace 1 variables
Run Code Online (Sandbox Code Playgroud)

此外,如果我按名称而不是索引引用列,则相同的赋值可以正常工作:

foo$bar <- bar
foo <- as.data.frame(list(bar=rep(5,5)))
bar <- as.POSIXlt(rep(5,5), origin="1970-1-1", tz="c")
foo$bar <- bar
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Rol*_*and 5

来自help("[.data.frame"):

对于[替换值可以是列表:列表的每个元素用于替换(一部分)一列,根据需要回收列表.

所以你可以做这样的事情:

df <- data.frame(a=1:2)
df[,2:3] <- list(3:4, 5:6)
#  a V2 V3
#1 1  3  5
#2 2  4  6
Run Code Online (Sandbox Code Playgroud)

但是,这会导致警告:

df[,4] <- list(7, 8)
# Warning message:
#   In `[<-.data.frame`(`*tmp*`, , 4, value = list(7, 8)) :
#   provided 2 variables to replace 1 variables
#   a V2 V3 V4
# 1 1  3  5  7
# 2 2  4  6  7
Run Code Online (Sandbox Code Playgroud)

现在一个POSIXlt对象是一个包含9个元素的列表:

unclass(rep(as.POSIXlt(Sys.time()), 2))
# $sec
# [1] 1.958244 1.958244
# 
# $min
# [1] 54 54
# 
# $hour
# [1] 10 10
# 
# $mday
# [1] 4 4
# 
# $mon
# [1] 9 9
# 
# $year
# [1] 113 113
# 
# $wday
# [1] 5 5
# 
# $yday
# [1] 276 276
# 
# $isdst
# [1] 1 1
# 
# attr(,"tzone")
# [1] ""     "CET"  "CEST"
Run Code Online (Sandbox Code Playgroud)

使用此列表将此列表分配给一列[<-.data.frame会显示您观察到的警告.

该问题的潜在解决方案很简单:

  • 使用POSIXct和避免POSIXlt.使用后者的唯一原因是需要提取一些列表组件,这通常不是这种情况(并且您可以始终强制使用POSIXlt例如内部函数来舍入时间值).
  • 使用$<-.data.frame.这在交互式使用之外通常很麻烦.
  • POSIXlt对象包裹在listfor作中:df[,1] <- list(POSIXlt_object).