简体   繁体   English

按天省略在年初和年末的日期

[英]Cut by day omits days at the beginning and end of the year

I'm trying to get the total number of observations with a certain color associated. 我正在尝试获取与某种特定颜色关联的观察总数。

Date <- c("2017-01-01","2017-01-01","2017-01-02","2017-01-03","2017-01-03","2017-01-03","2017-01-03","2017-01-04","2017-01-04","2017-01-05")

Factor<-c("red", "blue", "green", "red", "red", "green", "green", "blue", "blue", "green")

A different table for each of the factors. 每个因素都有一个不同的表。 They should all have one row for each day of the year and the total number of observations. 它们应该在一年中的每一天以及观察的总数上都排成一行。

green <-  table(cut(ex$Date[which(ex$Factor=="green")], 'day'))

It somehow works, but not perfectly. 它以某种方式起作用,但并不完美。 If there is no observation for 01.01.2017 and there is one for 02.01.2017 it will start the List with 02.01.2017. 如果没有对01.01.2017的观察,而对02.01.2017的观察,则它将以02.01.2017开头。 The data set is for one year. 数据集为一年。 The same problem appears at the end of the year (eg 31.12.2017, 30.12.2017,... being omitted). 相同的问题会在年底出现(例如,2017年12月31日,2017年12月30日,...被省略)。

2017-01-02   1
2017-01-03   2
2017-01-04   0
2017-01-05   1

Does anyone have an idea how I can solve this? 有人知道我该如何解决吗?

Here is one base R option using aggregate : 这是一个使用aggregate基本R选项:

df <- data.frame(Date, Color, stringsAsFactors=FALSE)
aggregate(df$Color, by=list(Date=df$Date), FUN=function(x) { sum(x=="green") })

        Date x
1 2017-01-01 0
2 2017-01-02 1
3 2017-01-03 2
4 2017-01-04 0
5 2017-01-05 1

Data: 数据:

Date <- c("2017-01-01","2017-01-01","2017-01-02","2017-01-03","2017-01-03","2017-01-03",
    "2017-01-03","2017-01-04","2017-01-04","2017-01-05")
Color <- c("red", "blue", "green", "red", "red", "green", "green", "blue", "blue", "green")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM