Skip to content
Prev 304261 / 398503 Next

saving and reading csv and rda files

On Mon, Aug 27, 2012 at 9:56 AM, Alok Bohara, PhD <bohara at unm.edu> wrote:
I personally find load() a little dangerous. It reloads objects with
the names they had when you saved them, potentially clobbering other
objects by that name. I'd recommend saveRDS/readRDS personally since
they don't clobber (they instead do what I think you think load()
does). That said,

data <- load(...) actually doesn't put the variables in data; rather
it puts them into the global envir by their original name and puts the
names as strings into data. I'd guess if you type data, you'll find
something like
[1] "y" "x1" "x2", "state_name"

You probably don't want to use "data" as a name in real code, because
it's the name of an important function used to, not surprisingly, load
data.

As far as the model fitting code, that should be covered in most basic
R tutorials, but you're looking for something like

lm( log(y) ~ x1)

Finally, I think you're a little tripped up on the multiple uses of
the term "names". "name" most commonly refers to variable name of an
object, but you seem to be confusing it with column names, which are
often recording what the variables in a data set are. The data set as
a unit has a "name" and then each of the recorded variates corresponds
to a column name. when you read/write csv files with header = TRUE,
the text keeps the column names, not the R object names.
Again, this doesn't work because load() doesn't return the loaded
objects, only the name.
This will work in that profit_data == data2, but profit_data isn't the
desired result of load("profit.rda").
Take a look at data2: it probably has "Q","L","K","country_name" as
_column_ names (not the same as object names). If it does, the
save/load cycle will preserve them.

Cheers,
Michael