【问题标题】:How do I write a function that counts the number of times a date occurs in my vector?如何编写一个函数来计算一个日期在我的向量中出现的次数?
【发布时间】:2021-02-09 03:06:50
【问题描述】:

我正在努力编写一个具有两个参数并最终打印出“DATE 发生 X 次事件”的函数。我正在使用 yyyy-mm-dd 格式的大量日期向量,但不知道从这里去哪里。我会附上我目前为止的工作。

occurrences_function<- function(Date, vector_of_dates) {
   Date<- ?
   X <- length(Date)
   output<- paste("Records show there were", X, "occurrences on", Date"
   return(output)
} 

【问题讨论】:

    标签: r function date


    【解决方案1】:

    我更喜欢对布尔向量求和:

    occurrences_function <- function(Date, vector_of_dates) {
        count <- sum(vector_of_dates == Date)
        output <- paste("Records show there were", count, "occurrences on", Date)
        return(output)
    }
    

    【讨论】:

    • 感谢您的快速回复!我试图连接布尔向量会是什么样子?我试图这样做,但它没有输出我想要它说的内容
    • 抱歉...没看到您要回复消息,请查看更新后的答案。
    猜你喜欢
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    相关资源
    最近更新 更多