Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

r - Getting the same value when estimating the mean across different categories

I am trying to estimate the mean value of a lab test reading igg1_norm across different categories of a variable type (5 categories).

    db <- forg %>% 
  group_by(forg$type)%>% 
  summarise(mean=mean(forg$igg1_norm, na.rm=TRUE),sd=sd(forg$igg1_norm, na.rm=TRUE),lower = mean(forg$igg1_norm, na.rm=TRUE) - sd(forg$igg1_norm, na.rm=TRUE), upper = mean(forg$igg1_norm, na.rm=TRUE) + sd(forg$igg1_norm, na.rm=TRUE))

My data looks like as per below

 cowidfarm type  time_num igg1_norm  igg2_norm 
   <chr>     <fct> <fct>    <labelled> <labelled>
 1 LM1047    3     1        0.1080482  0.4526854 
 2 LM1047    3     2        0.1833975  0.6029548 
 3 LM1047    3     3        0.1704118  0.5394913 
 4 LM1050    1     1        0.2883397  0.4347826 
 5 LM1050    1     2        0.1453905  0.5655340 
 6 LM1050    1     3        0.3302948  0.4962779 
 7 LM1134    3     1        0.4498922  0.6672078 
 8 LM1134    3     2        0.2641302  0.6204986 
 9 LM1134    3     3        0.3207913  0.5074442 
10 LM1221    3     1        1.2184955  0.8653846 

I get no errors when running the code but the output is odd as I get only one value when I was expecting 5 values (one for each category).

       mean        sd      lower     upper
1 0.4046562 0.3239133 0.08074287 0.7285695

Does anyone have an idea of what I am doing wrong? Any help is dearly appreciated

question from:https://stackoverflow.com/questions/66068409/getting-the-same-value-when-estimating-the-mean-across-different-categories

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not sure what you tried but when I remove forg$ from your code I get:

library(dplyr)

forg %>% 
  group_by(type)%>% 
  summarise(mean=mean(igg1_norm, na.rm=TRUE),sd=sd(igg1_norm, na.rm=TRUE),
            lower = mean(igg1_norm, na.rm=TRUE) - sd(igg1_norm, na.rm=TRUE), 
            upper = mean(igg1_norm, na.rm=TRUE) + sd(igg1_norm, na.rm=TRUE))
#> # A tibble: 2 x 5
#>    type  mean     sd   lower upper
#> * <int> <dbl>  <dbl>   <dbl> <dbl>
#> 1     1 0.255 0.0969 0.158   0.352
#> 2     3 0.388 0.383  0.00469 0.771

DATA

forg <- read.table(text = "cowidfarm type  time_num igg1_norm  igg2_norm 
  1 LM1047    3     1        0.1080482  0.4526854 
2 LM1047    3     2        0.1833975  0.6029548 
3 LM1047    3     3        0.1704118  0.5394913 
4 LM1050    1     1        0.2883397  0.4347826 
5 LM1050    1     2        0.1453905  0.5655340 
6 LM1050    1     3        0.3302948  0.4962779 
7 LM1134    3     1        0.4498922  0.6672078 
8 LM1134    3     2        0.2641302  0.6204986 
9 LM1134    3     3        0.3207913  0.5074442 
10 LM1221    3     1        1.2184955  0.8653846 ", header = TRUE)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...