我应该使用 R 或 Octave 在 csv 文件中进行日期操作吗?

发布于 12-28 14:52 字数 1431 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

盛夏已如深秋|2025-01-04 14:52:10

听起来好像您正在处理财务数据。 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.

我一向站在原地2025-01-04 14:52:10

您可以使用任何一种语言来操作日期,因此这主要取决于个人对语言的偏好。

我已经有一段时间没有使用 Octave 了,但我经常使用 R 和 MATLAB,在这两者中,我个人更喜欢 R 来进行数据操作(以及一般的数据处理任务)。如果您选择 R,lubridate 包是一个不错的起点。

You can manipulate dates in either, so it mostly boils down to personal preference for the language.

It's been a while since I used Octave, but I use R and MATLAB regularly, and of the two I personally prefer R for data manipulation (and data munging tasks generally). If you choose R, the lubridate package is a good place to start.

逐鹿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")

然后您可以根据您的问题 。我只是想给你一个起点。祝你好运!

I have never used Octave but I use R for data manipulation particulary csv files with Date as the first column and so far I am happy with it.
The functions I suggest while working with date is strptime function. After you load your csv data frame, conver the date character to date. This is an example:

 % if Date is in the first column
df$Date<-strptime(as.character(df[,"Date"]),tz="CET",format="%d-%m-%Y %H:%M")

you can then extract the day, the month and the year using

year<-format(df$Date,"%Y")
month<-format(df$Date,"%m")
day<-format(df$Date,"%d")

many more...depending on your problem. I just tried to give you a starting point. Good luck!

我爱人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))
    }
  }
}

Assuming that the data looks like:

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

Then you can try the following:

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))
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文