我正在尝试创建一个辅助函数,该函数将根据参数计算data.frame中有多少行。
getTotalParkeds <- function(place, weekday, entry_hour){
data <- PARKEDS[
PARKEDS$place == place,
PARKEDS$weekday == weekday,
PARKEDS$entry_hour == entry_hour
]
return(nrow(data))
}
然后我像这样运行
getTotalParkeds('MyPlace', 'mon', 1)
因此它返回此错误:
Warning: Error in : Length of logical index vector must be 1 or 11 (the number of columns), not 10000
我对R完全陌生,所以我不知道发生了什么。
答案 0 :(得分:2)
这是您需要采用的更正方法-
getTotalParkeds <- function(place, weekday, entry_hour){
data <- PARKEDS[
PARKEDS$place == place &
PARKEDS$weekday == weekday &
PARKEDS$entry_hour == entry_hour,
]
return(nrow(data))
}
答案 1 :(得分:0)
允许使用不同的PARKEDS
数据,例如下个月的数据:
getTotalParkeds <- function(input, place, weekday, entry_hour){
row.count <- nrow(subset(input, place == place &
weekday == weekday &
entry_hour == entry_hour))
return(row.count)
}