出现最大值后一个月的剩余天数

时间:2018-12-22 13:42:28

标签: r

我有一个这样的面板数据框

+

现在从上面的面板数据中,我想找到一个名为DMAX的变量。 DMAX是指天的单位,即最大回报日与同一个月的最后一个交易日之间的差额。例如,在1988年1月,公司A的最大收益出现在1988年1月12日。因此,DMAX是1988年1月12日到该月底之间的天数,即15天。 对于公司B,最大值出现在1988年1月22日。因此,该月的剩余天数是6天。因此,预期结果是

$("#content_div").html('<a href="'+knowledge_info+'"><h4 class="knowledge_base">Knowledge Info</h4></a>');

如果您能在这方面帮助我,我将不胜感激。

3 个答案:

答案 0 :(得分:1)

使用dplyr软件包的一种方法是以下方法。我称您的数据为mydf。首先,操纵date。然后,按datefirms对数据进行分组。然后,您在return中查找具有最大值的行并处理减法。

mutate(mydf, date = format(as.Date(date, format = "%d/%m/%Y"), "%m-%Y")) %>%
group_by(date, firms) %>%
summarize(DMAX = n() - which.max(return))

# A tibble: 2 x 3
# Groups:   date [?]
#  date    firms  DMAX
#  <chr>   <fct> <int>
#1 01-1988 A        15
#2 01-1988 B         6

数据

mydf <-structure(list(date = structure(c(18L, 19L, 20L, 21L, 22L, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L), .Label = c("11/1/1988", 
"12/1/1988", "13/01/1988", "14/01/1988", "15/01/1988", "16/01/1988", 
"18/01/1988", "19/01/1988", "20/01/1988", "21/01/1988", "22/01/1988", 
"23/01/1988", "25/01/1988", "26/01/1988", "27/01/1988", "28/01/1988", 
"29/01/1988", "5/1/1988", "6/1/1988", "7/1/1988", "8/1/1988", 
"9/1/1988"), class = "factor"), firms = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), 
return = c(5L, 6L, 4L, 5L, 6L, 6L, 13L, 3L, 2L, 5L, 2L, 7L, 
3L, 5L, 7L, 5L, 9L, 1L, 5L, 2L, 7L, 2L, 5L, 7L, 5L, 9L, 1L, 
5L, 2L, 7L, 2L, 5L, 6L, 8L, 5L, 4L, 3L, 18L, 5L, 2L, 7L, 
3L, 9L, 2L)), class = "data.frame", row.names = c(NA, -44L
))

答案 1 :(得分:0)

这是一个tidyverse解决方案。

library(tidyverse)
library(zoo)

df1 %>%
  mutate(date = dmy(date),
         month = as.yearmon(date)) %>%
  group_by(firms, month) %>%
  summarise(i = which(return == max(return)),
            DMAX = last(date) - date[last(i)]) %>%
  select(month, firms, DMAX)
## A tibble: 2 x 3
## Groups:   firms [2]
#  month         firms DMAX     
#  <S3: yearmon> <chr> <time>   
#1 Jan 1988      A     17 days  
#2 Jan 1988      B     " 7 days"

答案 2 :(得分:0)

1)基数R 对于每年/每月,公司汇总行数和最大收益行位置之间的差。不使用任何软件包。

$td.removeClass("ui-state-highlight")

给予:

mySelection: {},
loadComplete: function() {
  var $this = jQuery(this), gridParams = $this.jqGrid("getGridParam"),
      selectedCells = gridParams.mySelection,  rowId, tr, iCol, $td;

  for (rowId in selectedCells) {
      if (selectedCells.hasOwnProperty(rowId)) {
          tr = $this.jqGrid("getGridRowById", rowId);
          iCol = gridParams.iColByName[selectedCells[rowId]];
          $td = jQuery.jgrid.getCell.call(this, tr, iCol);
          $td.addClass("ui-state-highlight");
      }
  }
},
onCellSelect: function(rowId, iCol, cellContent, element) {
  var $this = jQuery(this),
      gridParams = $this.jqGrid("getGridParam"),
      selectedCells = gridParams.mySelection;
  if (selectedCells[rowId]) {
      // some sell is already selected in the row
      var tr = $this.jqGrid("getGridRowById", rowId),
          iColSelected = gridParams.iColByName[selectedCells[rowId]],
          $tdSelected = jQuery.jgrid.getCell.call(this, tr, iColSelected);
      $tdSelected.removeClass("ui-state-highlight");
      if (gridParams.iColByName[selectedCells[rowId]] === iCol) {
          // the current cell will be unselected
          delete selectedCells[rowId];
          return;
      }
  }
  // select the cell
  jQuery(element.target).closest("td").addClass("ui-state-highlight");
  // update column name in mySelection
  selectedCells[rowId] = gridParams.colModel[iCol].name;
},
beforeSelectRow: function(rowId, element) {
  return false;
},

2)动物园with(transform(DF, date = as.Date(date, "%d/%m/%Y")), aggregate(list(DMAX = return), data.frame(date = format(date, "%Y-%m"), firms), function(x) length(x) - which.max(x))) 读入动物园对象 date firms DMAX 1 1988-01 A 15 2 1988-01 B 6 ,每个公司有一列,然后按年/月汇总。最后使用DF将其融合为长格式的数据帧。如果动物园时间序列对象正常,则zd行可以省略。

fortify.zoo

给予:

fortify.zoo

请注意,library(zoo) zd <- read.zoo(DF, index = "date", format = "%d/%m/%Y", split = "firms") ag <- aggregate(zd, as.yearmon, function(x) length(na.omit(x)) - which.max(na.omit(x))) fortify.zoo(ag, melt = TRUE) 是以下格式的每月动物园系列:

     Index Series Value
1 Jan 1988      A    15
2 Jan 1988      B     6

3)data.table

ag

给予:

> ag
          A B
Jan 1988 15 6

注意

library(data.table)

DT <- as.data.table(DF)
DT[, list(DMAX = .N - which.max(return)), 
       by = list(date = format(as.Date(date, "%d/%m/%Y"), "%Y-%m"), firms)]