Erase content of dataframe in a single stroke

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Erase content of dataframe in a single stroke

Luigi
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Erase content of dataframe in a single stroke

Jim Lemon-4
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Erase content of dataframe in a single stroke

Jim Lemon-4
Ah, yes, try 'as.data.frame" on it.

Jim

On Thu, Sep 27, 2018 at 6:00 PM Luigi Marongiu <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
> > >
> > > ______________________________________________
> > > [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Erase content of dataframe in a single stroke

PIKAL Petr
Hm

I would use

> testdf<-data.frame(A=c(1,2),B=c(2,3),C=c(3,4))
> str(testdf)
'data.frame':   2 obs. of  3 variables:
 $ A: num  1 2
 $ B: num  2 3
 $ C: num  3 4
> testdf<-testdf[-(1:nrow(testdf)),]
> str(testdf)
'data.frame':   0 obs. of  3 variables:
 $ A: num
 $ B: num
 $ C: num

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Jim Lemon
> Sent: Thursday, September 27, 2018 10:12 AM
> To: Luigi Marongiu <[hidden email]>; r-help mailing list <r-help@r-
> project.org>
> Subject: Re: [R] Erase content of dataframe in a single stroke
>
> Ah, yes, try 'as.data.frame" on it.
>
> Jim
>
> On Thu, Sep 27, 2018 at 6:00 PM Luigi Marongiu <[hidden email]>
> 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 <[hidden email]>
> 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
> <[hidden email]> 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
> > > >
> > > > ______________________________________________
> > > > [hidden email] 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
>
> ______________________________________________
> [hidden email] 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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Erase content of dataframe in a single stroke

Jim Lemon-4
In reply to this post by Jim Lemon-4
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 <[hidden email]> 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 <[hidden email]> wrote:
> >
> > Ah, yes, try 'as.data.frame" on it.
> >
> > Jim
> >
> > On Thu, Sep 27, 2018 at 6:00 PM Luigi Marongiu <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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
> > > > >
> > > > > ______________________________________________
> > > > > [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Erase content of dataframe in a single stroke

Jan van der LAan-2
In reply to this post by PIKAL Petr
Or

testdf <- testdf[FALSE, ]

or

testdf <- testdf[numeric(0), ]

which seems to be slightly faster.

Best,
Jan


Op 27-9-2018 om 10:32 schreef PIKAL Petr:

> Hm
>
> I would use
>
>> testdf<-data.frame(A=c(1,2),B=c(2,3),C=c(3,4))
>> str(testdf)
> 'data.frame':   2 obs. of  3 variables:
>   $ A: num  1 2
>   $ B: num  2 3
>   $ C: num  3 4
>> testdf<-testdf[-(1:nrow(testdf)),]
>> str(testdf)
> 'data.frame':   0 obs. of  3 variables:
>   $ A: num
>   $ B: num
>   $ C: num
>
> Cheers
> Petr
>
>> -----Original Message-----
>> From: R-help <[hidden email]> On Behalf Of Jim Lemon
>> Sent: Thursday, September 27, 2018 10:12 AM
>> To: Luigi Marongiu <[hidden email]>; r-help mailing list <r-help@r-
>> project.org>
>> Subject: Re: [R] Erase content of dataframe in a single stroke
>>
>> Ah, yes, try 'as.data.frame" on it.
>>
>> Jim
>>
>> On Thu, Sep 27, 2018 at 6:00 PM Luigi Marongiu <[hidden email]>
>> 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 <[hidden email]>
>> 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
>> <[hidden email]> 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
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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
>> ______________________________________________
>> [hidden email] 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.
> Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
> Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Erase content of dataframe in a single stroke

Dénes Tóth-2
Hi Luigi,

Actually I doubt that the original problem you try to solve requires the
initialization of an empty data.frame with a particular structure.
However, if you think you really need this step, I would write a
function for it and also consider edge cases.

getSkeleton <- function(x, drop_levels = FALSE) {
   out <- x[numeric(0L), , drop = FALSE]
   if (isTRUE(drop_levels)) out <- droplevels(out)
   out
}

Note that it retains or drops factor levels depending on 'drop_levels'.
It only matters if you have factors in your data.frame.
'drop = FALSE' is required to guard against silent conversion to a
vector if 'x' has only one column.

Regards,
Denes



On 09/27/2018 11:11 AM, Jan van der Laan wrote:

> Or
>
> testdf <- testdf[FALSE, ]
>
> or
>
> testdf <- testdf[numeric(0), ]
>
> which seems to be slightly faster.
>
> Best,
> Jan
>
>
> Op 27-9-2018 om 10:32 schreef PIKAL Petr:
>> Hm
>>
>> I would use
>>
>>> testdf<-data.frame(A=c(1,2),B=c(2,3),C=c(3,4))
>>> str(testdf)
>> 'data.frame':   2 obs. of  3 variables:
>>   $ A: num  1 2
>>   $ B: num  2 3
>>   $ C: num  3 4
>>> testdf<-testdf[-(1:nrow(testdf)),]
>>> str(testdf)
>> 'data.frame':   0 obs. of  3 variables:
>>   $ A: num
>>   $ B: num
>>   $ C: num
>>
>> Cheers
>> Petr
>>
>>> -----Original Message-----
>>> From: R-help <[hidden email]> On Behalf Of Jim Lemon
>>> Sent: Thursday, September 27, 2018 10:12 AM
>>> To: Luigi Marongiu <[hidden email]>; r-help mailing list
>>> <r-help@r-
>>> project.org>
>>> Subject: Re: [R] Erase content of dataframe in a single stroke
>>>
>>> Ah, yes, try 'as.data.frame" on it.
>>>
>>> Jim
>>>
>>> On Thu, Sep 27, 2018 at 6:00 PM Luigi Marongiu
>>> <[hidden email]>
>>> 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 <[hidden email]>
>>> 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
>>> <[hidden email]> 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
>>>>>>
>>>>>> ______________________________________________
>>>>>> [hidden email] 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
>>> ______________________________________________
>>> [hidden email] 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.
>> Osobní údaje: Informace o zpracování a ochraně osobních údajů
>> obchodních partnerů PRECHEZA a.s. jsou zveřejněny na:
>> https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information
>> about processing and protection of business partner’s personal data
>> are available on website:
>> https://www.precheza.cz/en/personal-data-protection-principles/
>> Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou
>> důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení
>> odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any
>> documents attached to it may be confidential and are subject to the
>> legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Erase content of dataframe in a single stroke

Peter Dalgaard-2
In reply to this post by PIKAL Petr
Variations on the same theme:

> testdf<-data.frame(A=c(1,2),B=c(2,3),C=c(3,4))
> testdf[0,]
[1] A B C
<0 rows> (or 0-length row.names)
> testdf[FALSE,]
[1] A B C
<0 rows> (or 0-length row.names)
> testdf[integer(0),]
[1] A B C
<0 rows> (or 0-length row.names)
> testdf[NULL,]
[1] A B C
<0 rows> (or 0-length row.names)

-pd

> On 27 Sep 2018, at 10:32 , PIKAL Petr <[hidden email]> wrote:
>
> Hm
>
> I would use
>
>> testdf<-data.frame(A=c(1,2),B=c(2,3),C=c(3,4))
>> str(testdf)
> 'data.frame':   2 obs. of  3 variables:
> $ A: num  1 2
> $ B: num  2 3
> $ C: num  3 4
>> testdf<-testdf[-(1:nrow(testdf)),]
>> str(testdf)
> 'data.frame':   0 obs. of  3 variables:
> $ A: num
> $ B: num
> $ C: num
>
> Cheers
> Petr
>
>> -----Original Message-----
>> From: R-help <[hidden email]> On Behalf Of Jim Lemon
>> Sent: Thursday, September 27, 2018 10:12 AM
>> To: Luigi Marongiu <[hidden email]>; r-help mailing list <r-help@r-
>> project.org>
>> Subject: Re: [R] Erase content of dataframe in a single stroke
>>
>> Ah, yes, try 'as.data.frame" on it.
>>
>> Jim
>>
>> On Thu, Sep 27, 2018 at 6:00 PM Luigi Marongiu <[hidden email]>
>> 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 <[hidden email]>
>> 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
>> <[hidden email]> 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
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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
>>
>> ______________________________________________
>> [hidden email] 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.
> Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
> Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
>
> ______________________________________________
> [hidden email] 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.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.