-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 01/13/2011 02:56 PM, Duncan Murdoch wrote:
On 11-01-13 6:26 AM, Rainer M Krug wrote:
Hi
Assuming the following:
x<- data.frame(a=1:10, b=runif(10))
str(x)
'data.frame': 10 obs. of 2 variables:
$ a: int 1 2 3 4 5 6 7 8 9 10
$ b: num 0.692 0.325 0.634 0.16 0.873 ...
write.csv(x, "x.csv")
x2<- read.csv("x.csv")
str(x2)
'data.frame': 10 obs. of 3 variables:
$ X: int 1 2 3 4 5 6 7 8 9 10
$ a: int 1 2 3 4 5 6 7 8 9 10
$ b: num 0.692 0.325 0.634 0.16 0.873 ...
Using the two functions write.csv and read.csv, I would assume, that the
resulting data.frame x2 be identical with x, but it has an additional
column X, which contains the row names of x.
I know read.table and write.table which work as expected, but I would
like to use a csv for data exchange reasons.
I know that I can use
write.csv(x, "x.csv", row.names=FALSE)
and it would work, but shouldn't that be the default behaviour?
I don't think so. The CSV format is an export format which holds less
information than a dataframe. By exporting the dataframe to CSV and
importing the result, you are discarding information and you should
expect to get something different.
OK - I can follow this logic - and I think I can accept it.
If you want to save a dataframe to disk and read it back unchanged, you
should use save() and load().
And now my question from a previous thread (write.table equivalent for
lists?) comes up again:
using save() and load() definitely works, but it is highly unsave - as
it even keeps the names of the object, more then one can be saved, I can
not easily assign the saved object to a new name, I have problems using
the saved object if I have forgotten what the variable name was.
So I would like to expand my previous question: what are the proper
functions to store R objects? One could argue that all write...
functions are export functions - therefore keeping the data, but not
necessarily column names, rownames, attributes, ...
So what can I really do to save an R object for later usage in R?
Rainer
And if this is not compliant with csv files, shouldn't the function
read.csv convert the first column into the row names?
Cheers,
Rainer