「ボルチモア殺人事件」のデータセットの場合、「銃撃」などの文字列を受け取り、「銃撃」の犠牲者数を表す整数を返す関数を作成する必要があります。次の関数を書きましたが、エラーが発生します
エラー: " }" に予期しない '}' があります
エラー: オブジェクト 'counti' が見つかりません
==Nullが正しいかどうかもわかりません
count <- function(cause = NULL) {
## Check that "cause" is non-NULL; else throw error
if cause==NULL
{
stop()
print("no cause provided")
}
## Read "homicides.txt" data file
homicides <- readLines("homicides.txt")
## Extract causes of death
i <- grep(cause, homicides) ##get indices of cause
counti <- lenghth(i) ##get count of indices
## Check that specific "cause" is allowed; else throw error
if counti=0
{
stop()
print("no such cause")
}
## Return integer containing count of homicides for that cause
return(counti)
}
これは編集後の私の作業機能です、ありがとう
count <- function(cause = NULL) {
if(missing(cause) | is.null(cause)) stop("no cause provided")
homicides <- readLines("homicides.txt")
i=length(grep(cause, homicides))
if(i==0) stop("no cause found")
return(i)
}