if ( (i % 3 === 0) && (i % 4 === 0) ) {
console.log('Play Ball');
}
else if ( i % 3 === 0) {
console.log('Play');
}
else if ( i % 4 === 0) {
console.log('Ball');
}
假设这是我的数据集,我想创建一个新的数据框,每个年 - 月组合都有一个唯一的行,一个列计算该日期的每个住宅实例,第三列有每个商业的计数计数。例如,在这个数据集中我会有
IssuedDate PermitClassMapped
1 1981-12 Commercial
2 2012-12 Residential
3 1981-05 Residential
4 1981-05 Residential
5 1981-05 Commercial
6 1981-05 Commercial
我一直在搞乱tapply,count_()和其他没有真正好主意的功能。
答案 0 :(得分:2)
我们可以使用tidyr
和library(dplyr)
library(tidyr)
dt2 <- dt %>%
count(IssuedDate, PermitClassMapped) %>%
spread(PermitClassMapped, n, fill = 0)
dt2
# A tibble: 3 x 3
IssuedDate Commercial Residential
* <chr> <dbl> <dbl>
1 1981-05 2 2
2 1981-12 1 0
3 2012-12 0 1
。
dt <- read.table(text = "IssuedDate PermitClassMapped
1 1981-12 Commercial
2 2012-12 Residential
3 1981-05 Residential
4 1981-05 Residential
5 1981-05 Commercial
6 1981-05 Commercial",
header = TRUE, stringsAsFactors = FALSE)
数据强>
{{1}}