Error in for loop and IF function


See the FAQ: How to do a minimal reproducible example reprex for beginners; don't make reverse-engineering data objects an obstacle to attracting questions.

The immediate problem is that logical operators (see help(logic)

These operators act on raw, logical and number-like vectors

As @nirgrahamuk notes, vectorizing in dplyr is more fluid and succinct. It also returns a more useful object.

In using a for loop, seq_along is preferred over length and providing a "receiver" object avoids the surprise of receiving only the last value of [i].

my_coins <- c("eth","usdt","link","doge") 

mdata <- data.frame(symbol = c("usdt","doge"), current_price = c(1,2))
#>   symbol current_price
#> 1   usdt             1
#> 2   doge             2

result <- list()

for(i in seq_along(mdata)) 
  if (mdata$symbol[i] %in% my_coins) result[i] = mdata$current_price[i]

#> [1] 1 2


mdata %>% filter(symbol %in% my_coins)
#>   symbol current_price
#> 1   usdt             1
#> 2   doge             2