r语言 - 循环是魔鬼



在桌面RStudio中有这样的代码,一切都没问题:

arm_ids <- armband %>% summarise()
str(arm_ids)
for(item in seq_along(as.vector(arm_ids[[1]]))){
if((arm_ids[[1]][item] %in% arm_ids[[1]]) & (arm_ids[[1]][item] %in% all_activities[[1]]))
{
cat("Found item",item,"with id =",arm_ids[[1]][item],"n",sep=" ")
}
else print("Not found")
}

得到如下输出:

tibble [12 × 1] (S3: tbl_df/tbl/data.frame)
$ Id: num [1:12] 2.03e+09 2.35e+09 4.02e+09 4.39e+09 4.56e+09 ...
Found item 1 with id = 2026352035 
Found item 2 with id = 2347167796 
Found item 3 with id = 4020332650 
Found item 4 with id = 4388161847 
Found item 5 with id = 4558609924 
Found item 6 with id = 5553957443 
Found item 7 with id = 5577150313 
Found item 8 with id = 6117666160 
Found item 9 with id = 6775888955 
Found item 10 with id = 6962181067 
Found item 11 with id = 7007744171 
Found item 12 with id = 8792009665 

但是在Kaggle中完全相同的代码会产生一个错误:

tibble [1 × 0] (S3: tbl_df/tbl/data.frame)
Named list()
Error in `vec_as_location2_result()`:
! Can't subset columns past the end.
ℹ Location 1 doesn't exist.
ℹ There are only 0 columns.
Traceback:
1. as.vector(arm_ids[[1]])
2. arm_ids[[1]]
3. `[[.tbl_df`(arm_ids, 1)
4. tbl_subset2(x, j = i, j_arg = substitute(i))
5. vectbl_as_col_location2(j, length(x), j_arg = j_arg)
6. subclass_col_index_errors(vec_as_location2(j, n, names), j_arg = j_arg, 
.     assign = assign)
7. withCallingHandlers(expr, vctrs_error_subscript = function(cnd) {... ... ... ...

我的问题是:什么是错的,在哪里?

我真的不知道发生了什么,但是在重新粘贴代码并将Kaggle重置为出厂默认值后,代码开始工作了。

最新更新