【发布时间】:2018-06-25 13:31:15
【问题描述】:
我在带有 POSIXct 对象的 data.frame 上使用 apply。 问题是 apply 从将输入 data.frame 转换为.matrix() 开始(根据what I read)。这意味着 POSIXct 将被转换为 char。
我改用 lapply 解决了我的问题。但是有更好的解决方案吗?
# data.frame with one column of posixct
pos = data.frame(dateTime = c(Sys.time(), Sys.time(), Sys.time()))
str(pos) # POSIXct
test = function(x){
str(x[1])
return(x)
}
res = data.frame(apply(pos, 2, test))
str(res) # all strings
res2 = data.frame(lapply(pos, test))
str(res2) # all POSIXct
【问题讨论】:
标签: r type-conversion apply