首页 文章

计算具体日期[R]

提问于
浏览
0

有一个巨大的数据框,一列满了日期 . 像这样:

Date
2014-01-02
2014-01-02
2014-01-02
2014-01-03
2014-01-03
2014-02-01
2014-02-01
2014-02-02
2014-02-02

我想构建一个额外的列,其中包含当月出现的天数(!数据包含多年数据,因此有超过1个Januaries,Februaries等) . 像这样:

Date           Count
2014-01-02      5
2014-01-02      5
2014-01-02      5
2014-01-03      5
2014-01-03      5
2014-02-01      4
2014-02-01      4
2014-02-02      4
2014-02-02      4

我的解决方案很差 . 我使用过滤器选项(来自dplyr)来过滤特定月份,然后计算它们 . 但是因为它耗费了大量时间,而且因为我想自动执行此操作,所以我正在寻找更可持续的解决方案 .

4 回答

  • 1

    您可以使用base R

    d <- read.table(header=TRUE, stringsAsFactors = FALSE, text=
    "Date
    2014-01-02
    2014-01-02
    2014-01-02
    2014-01-03
    2014-01-03
    2014-02-01
    2014-02-01
    2014-02-02
    2014-02-02")
    
    d$count <- ave(!is.na(d$Date), substr(d$Date, 1,7), FUN=sum)
    d
    

    substr(d$Date, 1,7)d$Date 中的字符串中提取前七个字符(即包含年份和月份的部分,例如 2014-01 ) . 结果用于 ave() 中的分组

    这是 data.table 的解决方案:

    library("data.table")
    D <- fread(
    "Date
    2014-01-02
    2014-01-02
    2014-01-02
    2014-01-03
    2014-01-03
    2014-02-01
    2014-02-01
    2014-02-02
    2014-02-02")
    
    D[, count:=.N, substr(Date, 1, 7)]
    D
    
  • 2

    如果您的日期为POSIXlt格式,则内置月份,您可以创建一个表格来引用 .

    Date = as.POSIXlt(c('2014-01-02',
    '2014-01-02',
    '2014-01-02',
    '2014-01-03',
    '2014-01-03',
    '2014-02-01',
    '2014-02-01',
    '2014-02-02',
    '2014-02-02'))
    
    table(Date$mon)[as.character(Date$mon)]
    0 0 0 0 0 1 1 1 1 
    5 5 5 5 5 4 4 4 4
    

    0/1行只是列名 . 在POSIX 1月是0月,2月是1月等 .

  • 1

    既然你已经在使用 dplyr ......

    df <- read.table(text = "Date
    2014-01-02
                     2014-01-02
                     2014-01-02
                     2014-01-03
                     2014-01-03
                     2014-02-01
                     2014-02-01
                     2014-02-02
                     2014-02-02", header = T)
    df
    
    library(dplyr)
    df %>% 
      group_by(ym = format(as.Date(Date, "%Y-%m-%d"), "%Y-%m")) %>% 
      mutate(Count = n()) %>% 
      ungroup() %>%
      select(-ym)
    
  • 1

    我已经更改了您发布的示例,以便考虑每月的年份,因为您需要单独计算它们(在您的评论中指定):

    df = read.table(text = "
    Date
    2014-01-02
    2014-01-02
    2014-01-02
    2014-01-03
    2015-01-03
    2014-02-01
    2014-02-01
    2014-02-02
    2015-02-02",
    header=T)
    
    library(lubridate)
    library(dplyr)
    
    df %>%
      mutate(Date = ymd(Date)) %>%     # update to a datetime variable (if needed)
      group_by(Month = month(Date),    # for each month and year
               Year = year(Date)) %>%
      mutate(N = n()) %>%              # count number of rows/appearances
      ungroup() %>%                    # forget the grouping
      select(-Month, -Year)            # remove help variables
    
    # # A tibble: 9 x 2
    #         Date     N
    #       <date> <int>
    # 1 2014-01-02     4
    # 2 2014-01-02     4
    # 3 2014-01-02     4
    # 4 2014-01-03     4
    # 5 2015-01-03     1
    # 6 2014-02-01     3
    # 7 2014-02-01     3
    # 8 2014-02-02     3
    # 9 2015-02-02     1
    

相关问题