write.csv() extremely unexpected behavior

Viewed 70

Something explainable to me is going on. I have a dataframe:

> head(df)
  id   lon    lat   temp month year            hr     prec       ws
1  1 27.75 -22.25 295.35     9 2018 0.00007675205 401.1297 12.88135
2  2 28.25 -22.25 295.95     9 2018 0.00008084041 426.3411 12.89902
3  3 28.75 -22.25 296.25     9 2018 0.00008487972 449.7063 12.63242
4  4 29.25 -22.25 296.45     9 2018 0.00009112679 484.3495 12.59484
5  5 29.75 -22.25 296.65     9 2018 0.00009995372 533.0175 12.28485
6  6 30.25 -22.25 296.95     9 2018 0.00010895965 583.8255 11.80009

it looks like this:

> nrow(df)
[1] 607
> ncol(df)
[1] 9

when I do write.csv(df, /data/df.csv) it writes a humongous csv with tens of columns and thousands of rows. Has anyone experienced this kind of behavior? I rebooted my machine, restarted R, updated everything, and still persistently malicious, this keeps happening.

Output of dput(df): https://drive.google.com/file/d/1AkGK9Svwi9mSAcB0G3Ecx7aDC6ccnYRg/view?usp=sharing

2 Answers

str(x) will help you figure out what's going on.

x <- dget("fupedCSV.txt")
str(x)
## 'data.frame':    607 obs. of  9 variables:
## <a bunch of normal columns> ...
##  $ rh   :'data.frame':   607 obs. of  1 variable:
##   ..$ hr: num  7.68e-05 8.08e-05 8.49e-05 9.11e-05 1.00e-04 ...
##  $ prec :'data.frame':   607 obs. of  1 variable:
##   ..$ prec: num  401 426 450 484 533 ...
##  $ ws   :'data.frame':   607 obs. of  1 variable:
##   ..$ ws: num  12.9 12.9 12.6 12.6 12.3 ...

Note the last three columns, which are actually data frames nested inside the data frame.

## ORIGINAL: y <- as.data.frame(lapply(x, function(x) if (is.list(x)) x[[1]] else x ))
y <- do.call(data.frame,x)  ## thanks @akrun!
str(y)
## 'data.frame':    607 obs. of  9 variables:
##  $ id   : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ lon  : num  27.8 28.2 28.8 29.2 29.8 ...
##  $ lat  : num  -22.2 -22.2 -22.2 -22.2 -22.2 ...
##  $ temp : num  295 296 296 296 297 ...
##  $ month: int  9 9 9 9 9 9 9 9 9 9 ...
##  $ year : int  2018 2018 2018 2018 2018 2018 2018 2018 2018 2018 ...
##  $ rh   : num  7.68e-05 8.08e-05 8.49e-05 9.11e-05 1.00e-04 ...
##  $ prec : num  401 426 450 484 533 ...
##  $ ws   : num  12.9 12.9 12.6 12.6 12.3 ...

I haven't tested writing to a file, but I think this will clear up your problem.

The below does the trick.

do.call(data.frame,x) and
write.csv2(x,file="xxxx.csv", row.names=FALSE)

Related