发布评论
评论(4)
逐鹿2025-01-04 14:52:10
我从未使用过 Octave,但我使用 R 进行数据操作,特别是以日期作为第一列的 csv 文件,到目前为止我对此感到满意。
我在处理日期时建议使用的函数是 strptime 函数。加载 csv 数据框后,将日期字符转换为日期。这是一个示例:
% if Date is in the first column
df$Date<-strptime(as.character(df[,"Date"]),tz="CET",format="%d-%m-%Y %H:%M")
使用更多内容来提取日、月和年
year<-format(df$Date,"%Y")
month<-format(df$Date,"%m")
day<-format(df$Date,"%d")
然后您可以根据您的问题 。我只是想给你一个起点。祝你好运!
我爱人2025-01-04 14:52:10
假设数据如下:
date,attr1,attr2,attr3
"23/01/2011",1,2,3
"24/01/2011",4,5,6
"25/01/2011",7,8,9
"26/01/2011",10,11,12
"28/01/2011",13,45,55
"31/01/2011",2,2,2
那么您可以尝试以下操作:
data<-read.csv("yourfile.csv")
#not easy to insert new rows in data frame. So split data and dates
dates<-as.vector(data[[1]])
data<-as.matrix(data[,2:ncol(data)])
rows<-nrow(data)
for(i in 1:(rows-1)){
dd<-as.Date(dates[i],"%d/%m/%y%y")
dd1<-as.Date(dates[(i+1)],"%d/%m/%y%y")
diff<-dd1-dd
if (diff>1){
for (j in 1:(diff-1)){
new.date<-format(dd+j,format="%d/%m/%y%y")
dates[length(dates)+1]<-strtrim(paste(new.date,""),10)
data<-rbind(data,c(-1,-1,-1))
}
}
}
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
听起来好像您正在处理财务数据。 R 包 Zoo、xts 和 Quantmod 可能应该进行审查,因为它们为该领域的常见数据处理任务提供了可行的解决方案。还有其他定义财务日历的包。还有一个专门讨论此主题的 R-SIG 邮件列表。即使您正在处理一些其他现实场景,其数据仅限于非假日工作日,您仍然会在这些包中找到有用的功能来完成您(相当模糊地)概述的任务。
在 SO 上搜索“[r] 财务日历”会出现这个潜在相关的点击< /a> 以及其他几个。
It sounds as though you are dealing with financial data. The R packages zoo, xts, and quantmod should probably be reviewed because they offer worked solution to common data processing tasks in this area. There are other packages that define financial calendars. There is an R-SIG mailing list devoted to this topic as well. Even if you are dealing wihth some other real-world scenario that has data restricted to non-holiday weekdays, you are still going to find useful functionality in those packages for the task you (rather vaguely) have outlined.
Doing a search on SO for "[r] finance calendar" brings up this potentially relevant hit as well as several others.