首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >应用制造转换(POSIXct)

应用制造转换(POSIXct)
EN

Stack Overflow用户
提问于 2018-06-25 13:31:15
回答 1查看 42关注 0票数 2

我在带有data.frame对象的POSIXct上使用了apply。问题是,应用程序从转换输入data.frame as.matrix()开始(根据我所读到的)。这意味着POSIXct将被抛到焦炭中。

我用来解决问题。然而,是否有更好的解决办法?

代码语言:javascript
复制
    # 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-25 13:53:27

您可以使用来自map_df包的purrr函数。对于不同的输出类型,map函数有不同的版本,因此在本例中,您将使用map_df

代码语言:javascript
复制
# 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

library(purrr)

res3 = map_df(pos, test)
str(res3)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51024728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档