0

I have downloaded the daily prices for bitcoin as a .csv file, and the data is sorted from 2021-2017, but I want it to go from 2017 to 2021. My best solution was to reverse each column by indexing, but this is a bit time consuming with 10 columns per asset and at least 4 more assets to go. Is there a way to simply reverse the entire data set at once? I tried rev() and it just reversed the order of columns, which wasn't helpful.

BTC <- read.csv("Binance_BTCUSDT_d.csv", skip = 1)
  n1 <- nrow(BTC)
  BTC$date  <- as.Date(BTC$date[n1:1])
  BTC$open  <- BTC$open[n1:1]
  BTC$close <- BTC$close[n1:1]
  BTC$Volume.BTC <- BTC$Volume.BTC[n1:1]
  BTC_tradecount <- BTC$tradecount[n1:1]
  BTC$high <- NULL
  BTC$low <- NULL
  BTC$unix <- NULL
  BTC$symbol <- NULL
  BTC$Volume.USDT <- NULL
  BTC$tradecount <- NULL

I also had to remove some columns and did this "manually" as well. If there is any clever way to remove multiple columns at once, I would appreciate tips on this as well.

user438383
  • 5,716
  • 8
  • 28
  • 43
Sverre
  • 3
  • 3
  • Please, provide a [reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). There is o access to your data (.csv file). If you want to order rows, you could use `BTC <- BTC[order(date),]` – Mata Oct 13 '21 at 12:20

1 Answers1

2

For ordering rows, you can use this:

BTC <- BTC[order(BTC$date),]

To remove several columns at once, you can use the package dplyr:

library('dplyr')

BTC <- BTC %>% select(-high, -low, -unix, -symbol, -Volume.USDT, -tradecount)

If the column are ordered from high to tradecount, you can shorten it to:

BTC <- BTC %>% select(-(high:tradecount))
Mata
  • 538
  • 3
  • 17
  • Thank you! Removing columns worked great. I still have trouble with rearranging the columns. I've made a simple reproducible example: ``` df <- data.frame(date = c(2020, 2019, 2018), open = c(10, 12, 15), close = c(11, 12, 14)) ``` Basically I want to reverse the order so that it goes 2018 to 2020, and values also reverse to match its actual dates. ``` df <- df[order(date),] ``` I tried this, and I get: Error in order(date) : argument 1 is not a vector – Sverre Oct 13 '21 at 12:54
  • I edited my answer to `df <- df[order(df$date),] – Mata Oct 13 '21 at 13:03