Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputting a Dataframe in R to a .csv

Tags:

So I'm trying to write a .csv file based on a data frame in R, but for some reason I keep getting the following error:

Error in .External2(C_writetable, x, file, nrow(x), p, rnames, sep, eol,  :    unimplemented type 'list' in 'EncodeElement 

This is what traceback() is giving:

5: write.table(df, file = "df.csv", col.names = NA,         sep = ",", dec = ".", qmethod = "double") 4: eval(expr, envir, enclos) 3: eval(expr, p) 2: eval.parent(Call) 1: write.csv(df, file = "df.csv") 

Any solution?

like image 215
riders994 Avatar asked Jun 25 '13 07:06

riders994


People also ask

Can you export a data frame from R?

If your data frame is reasonably small, you can just use the write. csv function from base R to export it to a CSV file.

How do I import data into RStudio csv?

In RStudio, click on the Workspace tab, and then on “Import Dataset” -> “From text file”. A file browser will open up, locate the . csv file and click Open. You'll see a dialog that gives you a few options on the import.


2 Answers

You can also coerce data frames directly in R:

my.df <- data.frame(lapply(old.df, as.character), stringsAsFactors=FALSE) 

CAVEAT: this will coerce your entire dataframe to whatever type you specify. For example, if you want to coerce your dataframe to number, you would replace 'as.characater' with 'as.numeric':

 my.df <- data.frame(lapply(old.df, as.numeric), stringsAsFactors=FALSE) 
like image 67
SummerEla Avatar answered Sep 19 '22 08:09

SummerEla


One of your columns is of type list, so the data.frame is no longer 2-dimensional and can't be exported to a 2d csv-file.

If you still want to store the list in the resulting output, you might transform it to JSON first. So it becomes an column of type "character" which can be easily exported as one column to csv.

like image 20
Marcel Hebing Avatar answered Sep 19 '22 08:09

Marcel Hebing