R 如何在数据帧中每行应用xts函数
我在R中有一个数据帧,还有一个名为created_的列,其中保存了一个我想解析为datetime的文本。以下是一个快速预览:R 如何在数据帧中每行应用xts函数,r,time-series,xts,R,Time Series,Xts,我在R中有一个数据帧,还有一个名为created_的列,其中保存了一个我想解析为datetime的文本。以下是一个快速预览: head(pushes) created_at repo.url repository.url 1 2013-06-17T00:14:04Z https://github.com/Mindful/blog 2 2013-07-31T21:08:
head(pushes)
created_at repo.url repository.url
1 2013-06-17T00:14:04Z https://github.com/Mindful/blog
2 2013-07-31T21:08:15Z https://github.com/leapmotion/js.leapmotion.com
3 2012-11-04T07:08:15Z https://github.com/jplusui/jplusui
4 2012-06-21T08:16:22Z https://github.com/LStuker/puppet-rbenv
5 2013-03-10T09:15:51Z https://github.com/Fchaubard/CS108FinalProject
6 2013-10-04T11:34:11Z https://github.com/cmmurray/soccer
actor.login payload.actor actor_attributes.login
1 Mindful
2 joshbuddy
3 xuld
4 LStuker
5 ststanko
6 cmmurray
我写了一个说明,可以处理一些测试数据:
xts::.parseISO8601(“2012-06-17T00:14:04”,tz=“UTC”)$first.time
返回正确的Posix日期
但当我将其应用于包含以下说明的列时:
pushes$created_at <- xts::.parseISO8601(substr(pushes$created_at,1,nchar(pushes$created_at)-1),tz="UTC")$first.time
在处推送$created\u。parseISO8601
的第一个参数应该是字符串,而不是向量。您需要使用sapply
(或等效工具)在向量上循环
created_at <-
c("2013-06-17T00:14:04Z", "2013-07-31T21:08:15Z", "2012-11-04T07:08:15Z",
"2012-06-21T08:16:22Z", "2013-03-10T09:15:51Z", "2013-10-04T11:34:11Z")
# Only parses first element
.parseISO8601(substr(created_at,1,nchar(created_at)-1),tz="UTC")$first.time
# [1] "2013-06-17 00:14:04 UTC"
firstParseISO8601 <- function(x) .parseISO8601(x,tz="UTC")$first.time
# parse all elements
datetimes <- sapply(sub("Z$","",created_at), firstParseISO8601, USE.NAMES=FALSE)
# note that "simplifying" the output strips the POSIXct class, so we re-add it
datetimes <- .POSIXct(datetimes, tz="UTC")
创建于