我应该如何处理“ from”必须长度为1的错误? [英] How should I deal with 'from' must be of length 1 error?

查看:236
本文介绍了我应该如何处理“ from”必须长度为1的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算特定日期之间的天数。
我有2列所有字符向量。

I tried to count days between specific dates. I have 2 columns with all character vectors.

start.date <- c("2015-01-10","2015-01-11","2015-02-24")
end.date <- c("2015-03-10","2015-04-01","2015-06-13")
date10 <- data.frame(cbind(start.date,end.date))
date10$start.date <- as.character(date10$start.date)
date10$end.date <- as.character(date10$end.date)
str(date10)

,具体日期为2015-04-11至2015-07-10。
,所以我通过使用seq()将所有日期都设置在特定日期之间。
sp.da1< -ymd( 2015-04-11)
sp.da2< -ymd( 2015-07-10)
inteval.da< -seq(sp.da1,sp.da2,by ='day')
我想知道特定日期之间有多少天。
我尝试像上面一样使用seq(start.date,end.date,by ='day'),但出现此错误:'from'的长度必须为1

and the specific dates are from 2015-04-11 to 2015-07-10. so I made all date which is between the specific dates by using seq(). sp.da1<-ymd("2015-04-11") sp.da2<-ymd("2015-07-10") inteval.da<-seq(sp.da1, sp.da2, by = 'day') I wanted to know how many days are between the specific dates. I tried to use seq(start.date,end.date,by = 'day') like above, but I get this error: 'from' must be of length 1

请帮助我!

推荐答案

您正在询问给定时间间隔内有多少天一个主要的时间间隔。

You are asking how many days of a given time interval is inside a main time interval.

首先设置三个不同的时间间隔。然后,我们编写一个函数检查每天 x 是在主间隔内还是在主间隔外。如果我们将主间隔内的天数相加,就会得到您要搜索的内容:

Let's first set up the three varying time intervals. Then we write a function that checks for every day x whether it is inside or outside the main interval. If we sum up the number of days inside the main interval, we'll have what you're searching for:

date10$start.date <- as.Date.character(date10$start.date, format="%Y-%m-%d")
date10$end.date   <- as.Date.character(date10$end.date,   format="%Y-%m-%d")

your_intervals <- Map(seq, from = date10[, 1], to = date10[, 2], by = "days")

您的间隔

is_in_interval <- function(x, l_bound = sp.da1, u_bound = sp.da2){
  return (x > l_bound) & (x < u_bound)
}

sapply(your_intervals, function(x) sum(is_in_interval(x)))
# [1] 0 0 63

这篇关于我应该如何处理“ from”必须长度为1的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆