tibble question with a mean

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

tibble question with a mean

Erin Hodgess-2
Hello!

Here is a toy tibble problem:

xt <-
tibble(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
str(xt)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of  4 variables:
 $ x: chr  "A" "B" "C" "D"
 $ y: int  1 2 3 4
 $ z: num  0.3246 0.0504 0.339 0.4872
 $ a: chr  "dog" "cat" "tree" "ferret"
#No surprise
 xt %>% mean
[1] NA
Warning message:
In mean.default(.) : argument is not numeric or logical: returning NA
#surprised!
mean(xt[2:3])
[1] NA
Warning message:
In mean.default(xt[2:3]) : argument is not numeric or logical: returning NA
 xt[, 2:3] %>% mean
[1] NA
Warning message:
In mean.default(.) : argument is not numeric or logical: returning NA

I have a feeling that I'm doing something silly wrong.  Has anyone run into
this, please?  I saw something like this on this list, but didn't see a
solution.

Thanks,
Erin


Erin Hodgess, PhD
mailto: [hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[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: tibble question with a mean

plangfelder
I don't know tibble, so I'll do the same with a plain data frame:

a =
data.frame(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
> a
  x y           z      a
1 A 1 -0.08264865    dog
2 B 2  0.32344426    cat
3 C 3 -0.80416061   tree
4 D 4  1.27052529 ferret
> mean(a[2:3])
[1] NA
Warning message:
In mean.default(a[2:3]) : argument is not numeric or logical: returning NA
> mean(as.matrix(a[2:3]))
[1] 1.338395

The reason you get an error on mean(a[2:3]) is that a[2:3] is still a data
frame (a special list) and you cannot simply apply mean to a list. You need
to first convert to a matrix or vector which can then be fed to mean().

Peter


On Thu, Sep 20, 2018 at 5:50 PM Erin Hodgess <[hidden email]>
wrote:

> Hello!
>
> Here is a toy tibble problem:
>
> xt <-
> tibble(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
> str(xt)
> Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of  4 variables:
>  $ x: chr  "A" "B" "C" "D"
>  $ y: int  1 2 3 4
>  $ z: num  0.3246 0.0504 0.339 0.4872
>  $ a: chr  "dog" "cat" "tree" "ferret"
> #No surprise
>  xt %>% mean
> [1] NA
> Warning message:
> In mean.default(.) : argument is not numeric or logical: returning NA
> #surprised!
> mean(xt[2:3])
> [1] NA
> Warning message:
> In mean.default(xt[2:3]) : argument is not numeric or logical: returning NA
>  xt[, 2:3] %>% mean
> [1] NA
> Warning message:
> In mean.default(.) : argument is not numeric or logical: returning NA
>
> I have a feeling that I'm doing something silly wrong.  Has anyone run into
> this, please?  I saw something like this on this list, but didn't see a
> solution.
>
> Thanks,
> Erin
>
>
> Erin Hodgess, PhD
> mailto: [hidden email]
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.
>

        [[alternative HTML version deleted]]

______________________________________________
[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: [FORGED] Re: tibble question with a mean

Rolf Turner

Please see inline below.

n 09/21/2018 01:07 PM, Peter Langfelder wrote:

> I don't know tibble, so I'll do the same with a plain data frame:
>
> a =
> data.frame(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
>> a
>    x y           z      a
> 1 A 1 -0.08264865    dog
> 2 B 2  0.32344426    cat
> 3 C 3 -0.80416061   tree
> 4 D 4  1.27052529 ferret
>> mean(a[2:3])
> [1] NA
> Warning message:
> In mean.default(a[2:3]) : argument is not numeric or logical: returning NA
>> mean(as.matrix(a[2:3]))
> [1] 1.338395
>
> The reason you get an error on mean(a[2:3]) is that a[2:3] is still a data
> frame (a special list) and you cannot simply apply mean to a list. You need
> to first convert to a matrix or vector which can then be fed to mean().

Perhaps

sapply(a[2:3],mean)?

cheers,

Rolf

>
> Peter
>
>
> On Thu, Sep 20, 2018 at 5:50 PM Erin Hodgess <[hidden email]>
> wrote:
>
>> Hello!
>>
>> Here is a toy tibble problem:
>>
>> xt <-
>> tibble(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
>> str(xt)
>> Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of  4 variables:
>>   $ x: chr  "A" "B" "C" "D"
>>   $ y: int  1 2 3 4
>>   $ z: num  0.3246 0.0504 0.339 0.4872
>>   $ a: chr  "dog" "cat" "tree" "ferret"
>> #No surprise
>>   xt %>% mean
>> [1] NA
>> Warning message:
>> In mean.default(.) : argument is not numeric or logical: returning NA
>> #surprised!
>> mean(xt[2:3])
>> [1] NA
>> Warning message:
>> In mean.default(xt[2:3]) : argument is not numeric or logical: returning NA
>>   xt[, 2:3] %>% mean
>> [1] NA
>> Warning message:
>> In mean.default(.) : argument is not numeric or logical: returning NA
>>
>> I have a feeling that I'm doing something silly wrong.  Has anyone run into
>> this, please?  I saw something like this on this list, but didn't see a
>> solution.
>>
>> Thanks,
>> Erin

______________________________________________
[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: [FORGED] Re: tibble question with a mean

Erin Hodgess-2
Thanks to all for the good suggestions!!!

Erin Hodgess, PhD
mailto: [hidden email]


On Thu, Sep 20, 2018 at 7:38 PM Rolf Turner <[hidden email]> wrote:

>
> Please see inline below.
>
> n 09/21/2018 01:07 PM, Peter Langfelder wrote:
> > I don't know tibble, so I'll do the same with a plain data frame:
> >
> > a =
> >
> data.frame(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
> >> a
> >    x y           z      a
> > 1 A 1 -0.08264865    dog
> > 2 B 2  0.32344426    cat
> > 3 C 3 -0.80416061   tree
> > 4 D 4  1.27052529 ferret
> >> mean(a[2:3])
> > [1] NA
> > Warning message:
> > In mean.default(a[2:3]) : argument is not numeric or logical: returning
> NA
> >> mean(as.matrix(a[2:3]))
> > [1] 1.338395
> >
> > The reason you get an error on mean(a[2:3]) is that a[2:3] is still a
> data
> > frame (a special list) and you cannot simply apply mean to a list. You
> need
> > to first convert to a matrix or vector which can then be fed to mean().
>
> Perhaps
>
> sapply(a[2:3],mean)?
>
> cheers,
>
> Rolf
>
> >
> > Peter
> >
> >
> > On Thu, Sep 20, 2018 at 5:50 PM Erin Hodgess <[hidden email]>
> > wrote:
> >
> >> Hello!
> >>
> >> Here is a toy tibble problem:
> >>
> >> xt <-
> >> tibble(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
> >> str(xt)
> >> Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of  4 variables:
> >>   $ x: chr  "A" "B" "C" "D"
> >>   $ y: int  1 2 3 4
> >>   $ z: num  0.3246 0.0504 0.339 0.4872
> >>   $ a: chr  "dog" "cat" "tree" "ferret"
> >> #No surprise
> >>   xt %>% mean
> >> [1] NA
> >> Warning message:
> >> In mean.default(.) : argument is not numeric or logical: returning NA
> >> #surprised!
> >> mean(xt[2:3])
> >> [1] NA
> >> Warning message:
> >> In mean.default(xt[2:3]) : argument is not numeric or logical:
> returning NA
> >>   xt[, 2:3] %>% mean
> >> [1] NA
> >> Warning message:
> >> In mean.default(.) : argument is not numeric or logical: returning NA
> >>
> >> I have a feeling that I'm doing something silly wrong.  Has anyone run
> into
> >> this, please?  I saw something like this on this list, but didn't see a
> >> solution.
> >>
> >> Thanks,
> >> Erin
>

        [[alternative HTML version deleted]]

______________________________________________
[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: tibble question with a mean

David Carlson
In reply to this post by plangfelder
> xt[, 2:3] %>% colMeans
         y          z
 2.5000000 -0.4401625

> xt[2] %>% colMeans
  y
2.5
> t(xt[, 2]) %>% mean
[1] 2.5

-------------------------
David L. Carlson
Department of Anthropology
Texas A&M University

-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Peter Langfelder
Sent: Thursday, September 20, 2018 8:08 PM
To: Erin Hodgess <[hidden email]>
Cc: r-help <[hidden email]>
Subject: Re: [R] tibble question with a mean

I don't know tibble, so I'll do the same with a plain data frame:

a =
data.frame(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
> a
  x y           z      a
1 A 1 -0.08264865    dog
2 B 2  0.32344426    cat
3 C 3 -0.80416061   tree
4 D 4  1.27052529 ferret
> mean(a[2:3])
[1] NA
Warning message:
In mean.default(a[2:3]) : argument is not numeric or logical: returning NA
> mean(as.matrix(a[2:3]))
[1] 1.338395

The reason you get an error on mean(a[2:3]) is that a[2:3] is still a data
frame (a special list) and you cannot simply apply mean to a list. You need
to first convert to a matrix or vector which can then be fed to mean().

Peter


On Thu, Sep 20, 2018 at 5:50 PM Erin Hodgess <[hidden email]>
wrote:

> Hello!
>
> Here is a toy tibble problem:
>
> xt <-
> tibble(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
> str(xt)
> Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of  4 variables:
>  $ x: chr  "A" "B" "C" "D"
>  $ y: int  1 2 3 4
>  $ z: num  0.3246 0.0504 0.339 0.4872
>  $ a: chr  "dog" "cat" "tree" "ferret"
> #No surprise
>  xt %>% mean
> [1] NA
> Warning message:
> In mean.default(.) : argument is not numeric or logical: returning NA
> #surprised!
> mean(xt[2:3])
> [1] NA
> Warning message:
> In mean.default(xt[2:3]) : argument is not numeric or logical: returning NA
>  xt[, 2:3] %>% mean
> [1] NA
> Warning message:
> In mean.default(.) : argument is not numeric or logical: returning NA
>
> I have a feeling that I'm doing something silly wrong.  Has anyone run into
> this, please?  I saw something like this on this list, but didn't see a
> solution.
>
> Thanks,
> Erin
>
>
> Erin Hodgess, PhD
> mailto: [hidden email]
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.
>

        [[alternative HTML version deleted]]

______________________________________________
[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: tibble question with a mean

Erin Hodgess-2
David
That's awesome!

Thank you!!!

Erin Hodgess, PhD
mailto: [hidden email]


On Thu, Sep 20, 2018 at 9:19 PM David L Carlson <[hidden email]> wrote:

> > xt[, 2:3] %>% colMeans
>          y          z
>  2.5000000 -0.4401625
>
> > xt[2] %>% colMeans
>   y
> 2.5
> > t(xt[, 2]) %>% mean
> [1] 2.5
>
> -------------------------
> David L. Carlson
> Department of Anthropology
> Texas A&M University
>
> -----Original Message-----
> From: R-help [mailto:[hidden email]] On Behalf Of Peter
> Langfelder
> Sent: Thursday, September 20, 2018 8:08 PM
> To: Erin Hodgess <[hidden email]>
> Cc: r-help <[hidden email]>
> Subject: Re: [R] tibble question with a mean
>
> I don't know tibble, so I'll do the same with a plain data frame:
>
> a =
>
> data.frame(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
> > a
>   x y           z      a
> 1 A 1 -0.08264865    dog
> 2 B 2  0.32344426    cat
> 3 C 3 -0.80416061   tree
> 4 D 4  1.27052529 ferret
> > mean(a[2:3])
> [1] NA
> Warning message:
> In mean.default(a[2:3]) : argument is not numeric or logical: returning NA
> > mean(as.matrix(a[2:3]))
> [1] 1.338395
>
> The reason you get an error on mean(a[2:3]) is that a[2:3] is still a data
> frame (a special list) and you cannot simply apply mean to a list. You need
> to first convert to a matrix or vector which can then be fed to mean().
>
> Peter
>
>
> On Thu, Sep 20, 2018 at 5:50 PM Erin Hodgess <[hidden email]>
> wrote:
>
> > Hello!
> >
> > Here is a toy tibble problem:
> >
> > xt <-
> > tibble(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
> > str(xt)
> > Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of  4 variables:
> >  $ x: chr  "A" "B" "C" "D"
> >  $ y: int  1 2 3 4
> >  $ z: num  0.3246 0.0504 0.339 0.4872
> >  $ a: chr  "dog" "cat" "tree" "ferret"
> > #No surprise
> >  xt %>% mean
> > [1] NA
> > Warning message:
> > In mean.default(.) : argument is not numeric or logical: returning NA
> > #surprised!
> > mean(xt[2:3])
> > [1] NA
> > Warning message:
> > In mean.default(xt[2:3]) : argument is not numeric or logical: returning
> NA
> >  xt[, 2:3] %>% mean
> > [1] NA
> > Warning message:
> > In mean.default(.) : argument is not numeric or logical: returning NA
> >
> > I have a feeling that I'm doing something silly wrong.  Has anyone run
> into
> > this, please?  I saw something like this on this list, but didn't see a
> > solution.
> >
> > Thanks,
> > Erin
> >
> >
> > Erin Hodgess, PhD
> > mailto: [hidden email]
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [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.
> >
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.
>

        [[alternative HTML version deleted]]

______________________________________________
[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: tibble question with a mean

R help mailing list-2
In reply to this post by Erin Hodgess-2
Since you are using tibbles you may want to go whole-hog and use the dplyr
package as well.

> xt <-
tibble(x=LETTERS[1:4],y=1:4,z=log2(1:4),a=c("dog","cat","tree","ferret"))
> xt %>% summarize(yMean=mean(y), zMean=mean(z), aLast=last(a))
# A tibble: 1 x 3
  yMean zMean aLast
  <dbl> <dbl> <chr>
1   2.5  1.15 ferret


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Sep 20, 2018 at 5:50 PM, Erin Hodgess <[hidden email]>
wrote:

> Hello!
>
> Here is a toy tibble problem:
>
> xt <-
> tibble(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
> str(xt)
> Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of  4 variables:
>  $ x: chr  "A" "B" "C" "D"
>  $ y: int  1 2 3 4
>  $ z: num  0.3246 0.0504 0.339 0.4872
>  $ a: chr  "dog" "cat" "tree" "ferret"
> #No surprise
>  xt %>% mean
> [1] NA
> Warning message:
> In mean.default(.) : argument is not numeric or logical: returning NA
> #surprised!
> mean(xt[2:3])
> [1] NA
> Warning message:
> In mean.default(xt[2:3]) : argument is not numeric or logical: returning NA
>  xt[, 2:3] %>% mean
> [1] NA
> Warning message:
> In mean.default(.) : argument is not numeric or logical: returning NA
>
> I have a feeling that I'm doing something silly wrong.  Has anyone run into
> this, please?  I saw something like this on this list, but didn't see a
> solution.
>
> Thanks,
> Erin
>
>
> Erin Hodgess, PhD
> mailto: [hidden email]
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [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.
>

        [[alternative HTML version deleted]]

______________________________________________
[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: tibble question with a mean

Erin Hodgess-2
Nice!

I didn’t realize you could do all that!

Thanks!


On Fri, Sep 21, 2018 at 10:32 AM William Dunlap <[hidden email]> wrote:

> Since you are using tibbles you may want to go whole-hog and use the dplyr
> package as well.
>
> > xt <-
> tibble(x=LETTERS[1:4],y=1:4,z=log2(1:4),a=c("dog","cat","tree","ferret"))
> > xt %>% summarize(yMean=mean(y), zMean=mean(z), aLast=last(a))
> # A tibble: 1 x 3
>   yMean zMean aLast
>   <dbl> <dbl> <chr>
> 1   2.5  1.15 ferret
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
> On Thu, Sep 20, 2018 at 5:50 PM, Erin Hodgess <[hidden email]>
> wrote:
>
>> Hello!
>>
>> Here is a toy tibble problem:
>>
>> xt <-
>> tibble(x=LETTERS[1:4],y=1:4,z=rnorm(4),a=c("dog","cat","tree","ferret"))
>> str(xt)
>> Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of  4 variables:
>>  $ x: chr  "A" "B" "C" "D"
>>  $ y: int  1 2 3 4
>>  $ z: num  0.3246 0.0504 0.339 0.4872
>>  $ a: chr  "dog" "cat" "tree" "ferret"
>> #No surprise
>>  xt %>% mean
>> [1] NA
>> Warning message:
>> In mean.default(.) : argument is not numeric or logical: returning NA
>> #surprised!
>> mean(xt[2:3])
>> [1] NA
>> Warning message:
>> In mean.default(xt[2:3]) : argument is not numeric or logical: returning
>> NA
>>  xt[, 2:3] %>% mean
>> [1] NA
>> Warning message:
>> In mean.default(.) : argument is not numeric or logical: returning NA
>>
>> I have a feeling that I'm doing something silly wrong.  Has anyone run
>> into
>> this, please?  I saw something like this on this list, but didn't see a
>> solution.
>>
>> Thanks,
>> Erin
>>
>>
>> Erin Hodgess, PhD
>> mailto: [hidden email]
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [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.
>>
>
> --
Erin Hodgess, PhD
mailto: [hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[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.