[R] Erase content of data.frame in a single stroke

prof@@mit@mitt@l m@ili@g off gm@il@com prof@@mit@mitt@l m@ili@g off gm@il@com
Thu Sep 27 11:46:33 CEST 2018


I never bother with the dimensions of a data frame . That way you can assign
a new var before a for and auto assign it columns inside or nullify the
whole df instead of separate columns?

BR
-----Original Message-----
From: R-help <> On Behalf Of Jim Lemon
Sent: Thursday, September 27, 2018 2:06 PM
To: Luigi Marongiu <marongiu.luigi using gmail.com>; r-help mailing list
<r-help using r-project.org>
Subject: Re: [R] Erase content of dataframe in a single stroke

You're right. Apparently one can form a list with NULL elements but not a
data frame. I just saw Petr's answer, which seems to do the trick.

Jim
On Thu, Sep 27, 2018 at 6:19 PM Luigi Marongiu <marongiu.luigi using gmail.com>
wrote:
>
> I am not sure if I got it right; Now I get:
>
> >  toNull<-function(x) return(NULL)
> >  df<-as.data.frame(sapply(df,toNull))
> >  df
> data frame with 0 columns and 0 rows
> >  str(df)
> 'data.frame': 0 obs. of  0 variables
> On Thu, Sep 27, 2018 at 10:12 AM Jim Lemon <drjimlemon using gmail.com> wrote:
> >
> > Ah, yes, try 'as.data.frame" on it.
> >
> > Jim
> >
> > On Thu, Sep 27, 2018 at 6:00 PM Luigi Marongiu
<marongiu.luigi using gmail.com> wrote:
> > >
> > > Thank you Jim,
> > > this requires the definition of an ad hoc function; strange that R 
> > > does not have a function for this purpose...
> > > Anyway, it works but it changes the structure of the data. By 
> > > redefining the dataframe as I did, I obtain:
> > >
> > > > df
> > > [1] A B C
> > > <0 rows> (or 0-length row.names)
> > > > str(df)
> > > 'data.frame': 0 obs. of  3 variables:
> > >  $ A: num
> > >  $ B: num
> > >  $ C: num
> > >
> > > When applying your function, I get:
> > >
> > > > df
> > > $A
> > > NULL
> > >
> > > $B
> > > NULL
> > >
> > > $C
> > > NULL
> > >
> > > > str(df)
> > > List of 3
> > >  $ A: NULL
> > >  $ B: NULL
> > >  $ C: NULL
> > >
> > > The dataframe has become a list. Would that affect downstream
applications?
> > >
> > > Thank you,
> > > Luigi
> > > On Thu, Sep 27, 2018 at 9:45 AM Jim Lemon <drjimlemon using gmail.com>
wrote:
> > > >
> > > > Hi Luigi,
> > > > Maybe this:
> > > >
> > > > testdf<-data.frame(A=1,B=2,C=3)
> > > > > testdf
> > > >  A B C
> > > > 1 1 2 3
> > > > toNull<-function(x) return(NULL)
> > > > testdf<-sapply(testdf,toNull)
> > > >
> > > > Jim
> > > > On Thu, Sep 27, 2018 at 5:29 PM Luigi Marongiu
<marongiu.luigi using gmail.com> wrote:
> > > > >
> > > > > Dear all,
> > > > > I would like to erase the content of a dataframe -- but not 
> > > > > the dataframe itself -- in a simple and fast way.
> > > > > At the moment I do that by re-defining the dataframe itself in
this way:
> > > > >
> > > > > > df <- data.frame(A = numeric(),
> > > > > +                   B = numeric(),
> > > > > +                   C = character())
> > > > > > # assign
> > > > > > A <- 5
> > > > > > B <- 0.6
> > > > > > C <- 103
> > > > > > # load
> > > > > > R <- cbind(A, B, C)
> > > > > > df <- rbind(df, R)
> > > > > > df
> > > > >   A   B   C
> > > > > 1 5 0.6 103
> > > > > > # erase
> > > > > > df <- data.frame(A = numeric(),
> > > > > +                  B = numeric(),
> > > > > +                  C = character())
> > > > > > df
> > > > > [1] A B C
> > > > > <0 rows> (or 0-length row.names)
> > > > > >
> > > > >
> > > > > Is there a way to erase the content of the dataframe in a 
> > > > > simplier (acting on all the dataframe at once instead of 
> > > > > naming each column
> > > > > individually) and nicer (with a specific erasure command 
> > > > > instead of re-defyining the object itself) way?
> > > > >
> > > > > Thank you.
> > > > > --
> > > > > Best regards,
> > > > > Luigi
> > > > >
> > > > > ______________________________________________
> > > > > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, 
> > > > > see https://stat.ethz.ch/mailman/listinfo/r-help
> > > > > PLEASE do read the posting guide 
> > > > > http://www.R-project.org/posting-guide.html
> > > > > and provide commented, minimal, self-contained, reproducible code.
> > >
> > >
> > >
> > > --
> > > Best regards,
> > > Luigi
>
>
>
> --
> Best regards,
> Luigi

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list