0

I have a data frame data, which contains information about crypto currencies. I need to find a way to create a vector with symbols of cryptos, which absolute value of market capitalization change in 24h in percentage is at least 10. I have the following function:

pos_MCchange <- data$market_cap_change_percentage_24h >= 10
neg_MCchange <- data$market_cap_change_percentage_24h <= -10
vector <- c((data$symbol[pos_MCchange]), (data$symbol[neg_MCchange]))

It returns exactly what is required, but the symbols appear in the different order than in the data frame. Short example to illustrate my problem:

data
#   symbol market_cap_change_percentage_24h
# 1    a   11
# 2    b   9
# 3    c   -15
# 4    d   -5
# 5    e   12
# 6    f   6
# 7    g   -14

My function would return: c(11, 12, -15, -14). Yet, I want it to be like c(11, -15, 12, -14).

Is it possible to create the vector in a way that symbols (with the corresponding market cap. change) would appear in the same order as they are in the data frame? Please, let me know.

SSG_08
  • 47
  • 5
  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). Do you need this https://stackoverflow.com/questions/11977102/order-data-frame-rows-according-to-vector-with-specific-order ? – Ronak Shah May 04 '21 at 10:48

1 Answers1

0

I think you can use subset + abs to make it

subset(
  df,
  abs(market_cap_change_percentage_24h) >= 10
)

which gives

  symbol market_cap_change_percentage_24h
1      a                               11
3      c                              -15
5      d                               12
7      d                              -14
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81