我有以下功能:
histogram <- function()
eucaris_countries <- c("NL", "B")
counter_eucaris = 0
counter_niet_eucaris = 0
ikr_set <- read.csv2("IKR data.csv", header = T)
for (i in 1:nrow(ikr_set)){
if (ikr_set$LAND_HERK %in% eucaris_countries) {
counter_eucaris + 1
}
else {
counter_niet_eucaris +1
}
counter_eucaris
}
应该执行以下操作:
当我尝试加载脚本时,我得到一个错误:
Error in match(x, table, nomatch = 0L) : object 'eucaris_countries' not found
我不知道这里出了什么问题......有什么想法吗?
答案 0 :(得分:1)
你真的需要一个功能去做你想做的事吗?看起来你正在尝试计算变量在(而不是在)向量元素中的行数。你可以用这样的代码做到这一点......
# example fake data
ikr_set <- data.frame(LAND_HERK=c("A", "B", "B", "B", "C", "C", "NL"))
# elements of interest
eucaris_countries <- c("NL", "B")
sum(ikr_set$LAND_HERK %in% eucaris_countries)
sum(!(ikr_set$LAND_HERK %in% eucaris_countries))