0

I have a data set where the variable Tier can be 1, 2, 3 , or 4. I need to find the mean of Full.time for each type of Tier. Here is my code:

mean(my_data$Full.time ~ my_data$Tier)

and here is my error message:

[1] NA
Warning message:
In mean.default(my_data$Full.time ~ my_data$Tier) :
  argument is not numeric or logical: returning NA

(I also have to do the same thing with standard deviation but I assume the correct answer will be the same with sd() instead of mean())

Here is the file:

Text File

smci
  • 32,567
  • 20
  • 113
  • 146
Hannah
  • 9
  • 6
  • 2
    The answers to [this question](https://stackoverflow.com/questions/21982987/mean-per-group-in-a-data-frame) are a terrific summary of the options available for getting means by group. – A. S. K. Dec 11 '19 at 19:50
  • 2
    Does this answer your question? [Mean per group in a data.frame](https://stackoverflow.com/questions/21982987/mean-per-group-in-a-data-frame) – A. S. K. Dec 11 '19 at 19:50
  • 3
    [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making an R question that folks can help with. That includes a sample of data, all necessary code, and a clear explanation of what you're trying to do and what hasn't worked. That said, I don't think I've seen `mean` called on a formula...where did you get that? – camille Dec 11 '19 at 19:51
  • @A.S.K. I don't think so? This is what my textfile looks like – Hannah Dec 11 '19 at 20:13
  • @A.S.K. I'll edit the post and add the text file – Hannah Dec 11 '19 at 20:13
  • Try `aggregate(my_data$Full.time, list(my_data$Tier), mean)`; that should create the summary you're looking for. – A. S. K. Dec 11 '19 at 20:19
  • Probably because Tier 4 contains NAs somewhere; try `aggregate(my_data$Full.time, list(my_data$Tier), mean, na.rm = T)`. – A. S. K. Dec 11 '19 at 20:32

2 Answers2

0

Maybe the following code can make things you want:

  • if you want compact result of mean and standard deviation, you can use aggreate(), i.e.,
Mean <- aggregate(Full.time ~ Tier, my_data, mean)

and

SD <- aggregate(Full.time ~ Tier, my_data, sd)
  • if you want results of mean and standard deviation with the same rows of my_data, you can use ave(), i.e.,
Mean <- with(my_data, ave(Full.time, Tier, mean))

and

SD <- with(my_data, ave(Full.time, Tier, sd))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0

Here's the dplyr approach:

library(dplyr)

mtcars %>%
    group_by(Tier) %>%
    summarize(mean=mean(Full.time), sd=sd(Full.time))

Note that dplyr allows us to name each aggregation result column. And here's an illustration on mtcars, because we can't access your dataset:

data(mtcars)

mtcars %>% group_by(cyl) %>% summarize(mean_hp=mean(hp), sd_hp=sd(hp))

    cyl mean_hp sd_hp
  <dbl>   <dbl> <dbl>
1     4    82.6  20.9
2     6   122.   24.3
3     8   209.   51.0
smci
  • 32,567
  • 20
  • 113
  • 146