有没有办法让my_fun()自动使用/更改为'pear'和 当我将p_b_numbers插入函数时,'banana'在for循环中?所以当使用my_fun(p_b_numbers)时,输出将是pear [3] banana [1]。
a_o_numbers = c(10,10,60,60)
p_b_numbers = c(10,10,10,60)
apple = 0
orange = 0
pear = 0
banana = 0
my_fun <- function(y){
for(x in y){
if(x < 50){
apple <<- apple +1
}else{
orange <<- orange +1
}
}
}
my_fun(a_o_numbers)
apple
orange
输出: 苹果 [2] 橙子 [2]