Soph2010
Soph2010

Reputation: 613

R - Export large dataframe into CSV

Beginner here: I have a list (see screenshot) called Coins_list from which I want to export the second dataframe stored in it called data into a csv. When I use the code

write.csv(Coins_list$data, file = "Coins_list_full_data.csv")

I get a huge CSV with a bunch of numbers from the column named price which apparently containts more dataframes, if I read the output correctly or at least display the data in the price column? How can I export this dataframe into CSV correctly? See screenshot for more details. Screenshot

EDIT: I was able to get the first four rows into CSV by using df2 <- Coins_list$data write.csv(df2[1:4,], file="BTC_row.csv"), however it now looks like R puts the price of all four rows within a list c( ) and repeats it in each row? Any idea how to change that?

enter image description here

Upvotes: 0

Views: 357

Answers (1)

v.mascherini
v.mascherini

Reputation: 13

(I would post this as a comment but I have too few reputation)

Hey, you could try for starters to flatten the json file by going further than response list$content but looking at what's into the content with another $.

Else you could try getting data$price and see what pops up from there.

something like this:

names = list(data$symbol)

df = data.frame(price = NA, symbol = NA)

for (i in length(data)) {
  
  
  x = data.frame(price = data$price[i], symbol = names[i])
  
  df = inner_join(df, data)
  
  
}

to get a dataframe with price and symbol. I don't know how the data is nested so I'm just guessing.

It would be helpful to know from where you got the data for reproducibility.

Upvotes: 1

Related Questions