我有一个问题,我一直在工作。我有一个日期数据集(以数据框格式),我需要搜索并查找每个月的最后一天并将它们放入新的数据框中。下一栏中还有一个与之相关的值。下面是日期的样本。
[1] "2015-05-21" "2015-05-20" "2015-05-19" "2015-05-18" "2015-05-15" "2015-05-14"
[7] "2015-05-13" "2015-05-12" "2015-05-11" "2015-05-08" "2015-05-07" "2015-05-06"
[13] "2015-05-05" "2015-05-04" "2015-05-01" "2015-04-30" "2015-04-29" "2015-04-28"
[19] "2015-04-27" "2015-04-24" "2015-04-23" "2015-04-22" "2015-04-21" "2015-04-20"
[25] "2015-04-17" "2015-04-16" "2015-04-15" "2015-04-14" "2015-04-13" "2015-04-10"
[31] "2015-04-09" "2015-04-08" "2015-04-07" "2015-04-06" "2015-04-02" "2015-04-01"
[37] "2015-03-31" "2015-03-30" "2015-03-27" "2015-03-26" "2015-03-25" "2015-03-24"
[43] "2015-03-23" "2015-03-20" "2015-03-19" "2015-03-18" "2015-03-17" "2015-03-16"
[49] "2015-03-13" "2015-03-12" "2015-03-11" "2015-03-10" "2015-03-09" "2015-03-06"
[55] "2015-03-05" "2015-03-04" "2015-03-03" "2015-03-02" "2015-02-27" "2015-02-26"
[61] "2015-02-25" "2015-02-24" "2015-02-23" "2015-02-20" "2015-02-19" "2015-02-18"
[67] "2015-02-17" "2015-02-13" "2015-02-12" "2015-02-11" "2015-02-10" "2015-02-09"
[73] "2015-02-06" "2015-02-05" "2015-02-04" "2015-02-03" "2015-02-02" "2015-01-30"
[79] "2015-01-29" "2015-01-28" "2015-01-27" "2015-01-26" "2015-01-23" "2015-01-22"
[85] "2015-01-21" "2015-01-20" "2015-01-16" "2015-01-15" "2015-01-14" "2015-01-13"
[91] "2015-01-12" "2015-01-09" "2015-01-08" "2015-01-07" "2015-01-06" "2015-01-05"
[97] "2015-01-02" "2014-12-31" "2014-12-30" "2014-12-29" "2014-12-26" "2014-12-24"
[103] "2014-12-23" "2014-12-22" "2014-12-19" "2014-12-18" "2014-12-17" "2014-12-16"
[109] "2014-12-15" "2014-12-12" "2014-12-11" "2014-12-10" "2014-12-09" "2014-12-08"
[115] "2014-12-05" "2014-12-04" "2014-12-03" "2014-12-02" "2014-12-01" "2014-11-28"
[121] "2014-11-26" "2014-11-25" "2014-11-24" "2014-11-21" "2014-11-20" "2014-11-19"
[127] "2014-11-18" "2014-11-17" "2014-11-14" "2014-11-13" "2014-11-12" "2014-11-11"
[133] "2014-11-10" "2014-11-07" "2014-11-06" "2014-11-05" "2014-11-04" "2014-11-03"
[139] "2014-10-31" "2014-10-30" "2014-10-29" "2014-10-28" "2014-10-27" "2014-10-24"
[145] "2014-10-23" "2014-10-22" "2014-10-21" "2014-10-20" "2014-10-17" "2014-10-16"
[151] "2014-10-15" "2014-10-14" "2014-10-13" "2014-10-10" "2014-10-09" "2014-10-08"
[157] "2014-10-07" "2014-10-06" "2014-10-03" "2014-10-02" "2014-10-01" "2014-09-30"
[163] "2014-09-29" "2014-09-26" "2014-09-25" "2014-09-24" "2014-09-23" "2014-09-22"
[169] "2014-09-19" "2014-09-18" "2014-09-17" "2014-09-16" "2014-09-15" "2014-09-12"
这是一小部分。有5700行...
答案 0 :(得分:9)
1)请尝试@Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
ImageVIew view ...;
Drawable drawable = view.getBackground();
Rect myRect = drawable.getBounds();
if (myRect.contains((int) event.getX(), (int) event.getY()))
{
//you can compare the distance and choose what to do !
//means pressed on your imageView
}
return true;
}
else
{
return false;
}
}
,如下所示:
tapply
或者这个:
dates <- c("2015-05-13", "2015-05-12", "2015-05-11", "2015-04-27",
"2015-04-24", "2015-04-23")
tapply(dates, substr(dates, 1, 7), max)
2)如果日期在数据框中:
library(zoo)
tapply(dates, as.yearmon(dates), max)
或将 DF <- data.frame(dates, stringsAsFactors = FALSE)
aggregate(DF["dates"], list(month = substr(DF$dates, 1, 7)), max)
替换为substr(...)
。
3)这会挑出每个月的最后一行。在这种情况下,as.yearmon(DF$dates)
返回一个字符变量,因此需要使用ave
将其转换为逻辑:
as.logical
或使用isMax <- function(x) seq_along(x) == which.max(as.Date(x))
subset(DF, as.logical(ave(dates, substr(dates, 1, 7), FUN = isMax)))
代替as.yearmon(dates)
。 substr(...)
的以下定义可以替换
isMax
并给出相同的结果,除非有多个最大值。在这种情况下,它只给出第一个,而早期的定义给出了所有。由于isMax <- function(x) seq_along(x) == which.max(as.Date(x))
不适用于which.max
类,因此我们将日期转换为"character"
类。
下次请使用"Date"
显示您的数据或以其他方式提供可重复的格式,如上所述。同时将其降低到说明问题所需的最小量。
答案 1 :(得分:3)
以下是一种替代解决方案,使用dplyr
按月和年分组,然后过滤max
,即每年的最后日期:
df <- data.frame(dates=as.Date(c("2015-05-21", "2015-05-20", "2015-05-19", "2015-05-18",
"2015-05-15", "2015-05-14","2014-12-05", "2014-12-04", "2014-12-03",
"2014-12-02", "2014-12-01", "2014-11-28")))
library(dplyr)
df %>%
group_by(strftime(dates, "%Y-%m")) %>% #Groups by the yearmonths
filter(dates == max(dates)) %>% #Take the last date of each group
.$dates #Returns the filtered dates as a vector
导致:
[1] "2015-05-21" "2014-12-05" "2014-11-28"
感谢@akrun提供有关修复和改进代码的提示。
答案 2 :(得分:0)
如果你有一个数据框,&#39; a&#39;包括日期变量,&#39; date&#39;,
ClassNotFoundExeption
例如:
library(dplyr)
a<-mutate(a,endmonth=as.Date(paste0(as.numeric(format(date,"%Y%m"))+1,"01"),"%Y%m%d")-1)