Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)
edited by

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 execute the write.csv(df, /data/df.csv) it writes a humongous csv with tons 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.

The output of dput(df).

1 Answer

0 votes
by (108k points)
edited by

In R programming, the str(x) will guide you to find out about the structure of the data:

x <- dget("fupedCSV.txt")

str(x)

## 'data.frame':    607 obs. of  9 variables:

## <a > ...

##  $ 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 ...

Now from the structure, see that the last three columns, the data frames are 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 think this observation has cleared up your query.

Related questions

Browse Categories

...