我在带有data.frame对象的POSIXct上使用了apply。问题是,应用程序从转换输入data.frame as.matrix()开始(根据我所读到的)。这意味着POSIXct将被抛到焦炭中。
我用来解决问题。然而,是否有更好的解决办法?
# 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发布于 2018-06-25 13:53:27
您可以使用来自map_df包的purrr函数。对于不同的输出类型,map函数有不同的版本,因此在本例中,您将使用map_df
# 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)https://stackoverflow.com/questions/51024728
复制相似问题