Scott Davis
Scott Davis

Reputation: 993

Error exporting data.frame as csv

Exporting data.frame as .csv with code.

write.csv(df, "name.csv")

LogitTV.Rda has 3000 rows and 4 columns.

My code has an error when identifying the data.frame.

load("~/Home Automation/LogitTV.Rda")
write.csv(LogitTV.Rda, "LogitTV.csv")

Error in is.data.frame(x) : object 'LogitTV.Rda' not found

Checked the following:

1) Cleaned the console of previous history

2) Working Directory set as ~/Home Automation/

Anything else to check for preventing the error?

Thanks

Upvotes: 1

Views: 2325

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226017

LogitTV.Rda is, confusingly, not the name of the object that gets loaded.

Try:

loadedObj <- load("~/Home Automation/LogitTV.Rda")
write.csv(get(loadedObj), file="LogitTV.csv")

This assumes that the .Rda file contains only a single R object, and that it is a data frame or matrix.

It would be nice if write.csv had a way to accept the name of an object instead of the object itself (so get() was unnecessary), but I don't know of one.

Upvotes: 3

Related Questions