首页 文章

计算包含特定年份的NUM行数

提问于
浏览
-2

我有一个数据框,其中包含此格式的日期列(1990-02-28)我想计算包含1996年的行数(无论月/日) .

例如:

DF

1. 1946-01-21   -0.7062
2. 1986-01-22   0.5029
3. 1923-01-23   0.5657
4. 1920-01-25   0.4723
5. 1996-01-26   -0.5384
6. 1996-01-27   0.717

响应为2(#5,#6)

谢谢

4 回答

  • 0
    library(lubridate)
    library(dplyr)
    
    dt = read.table(text = "
    date value
    19460121 -0.7062
    19860122 0.5029
    19230123 0.5657
    19200125 0.4723
    19960126 -0.5384
    19960127 0.717
    ", header = T)
    
    dt %>%
      mutate(date = ymd(date)) %>%     # make this a date column (if it's not already) 
      filter(year(date) == 1996) %>%   # filter rows where the year is 1996
      nrow()                           # count rows
    
  • 0

    基础R的其他方式:

    df=read.table(text="
    19460121 -0.7062
    19860122 0.5029
    19230123 0.5657
    19200125 0.4723
    19960126 -0.5384
    19960127 0.717")
    
    df$V1=as.character(df$V1)
    
    table(format(as.Date(df$V1,"%Y%m%d"),"%Y"))
    #1920 1923 1946 1986 1996 
    #   1    1    1    1    2 
    
    table(format(as.Date(df$V1,"%Y%m%d"),"%Y"))["1996"]
    #1996 
    #   2
    
  • 1

    仅列操作可能更快 . 以下是其中3个:

    read.table(stringsAsFactors=FALSE, header=FALSE, text="
    19460121 -0.7062
    19860122 0.5029
    19230123 0.5657
    19200125 0.4723
    19960126 -0.5384
    19960127 0.717") -> xdf
    
    # base R
    sum(grepl("^1996", xdf$V1))
    
    # stringi one way
    sum(!is.na(stringi::stri_match_first_regex(xdf$V1, "^1996")))
    
    # stringi another way
    sum(stringi::stri_count_regex(xdf$V1, "^1996"))
    
  • 0

    在基础R语句中

    DF[ grepl( "1996", DF[ , 1 ] ), ]
    

    会实现你的目标:

    > DF[ grepl( "1996", DF[ , 1 ] ), ]
          date   value
    5 19960126 -0.5384
    6 19960127  0.7170
    

    编辑:

    可以找到行数

    nrow( DF[ grepl( "1996", DF[ , 1 ] ), ] )
    

    或正确使用 length()

    length( DF[ grepl( "1996", DF[ , 1 ] ), 1 ] )
    

相关问题