[R] tibble question with a mean

William Dunlap wdun|@p @end|ng |rom t|bco@com
Fri Sep 21 18:32:33 CEST 2018


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 <erinm.hodgess using gmail.com>
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: erinm.hodgess using gmail.com
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]




More information about the R-help mailing list