Home > Blockchain >  What I am missing using group by?
What I am missing using group by?

Time:12-20

I am working with Rstudio on a dataset that includes two treatments and a response that is in Kg. I am trying to summarize and get the mean of Kg by TRT. However, I got the same values for both responses. My dataset is much bigger than the data enclosed below but are the same variables.

However, when I work with those nine values, I get the real response, but I get the same mean for both when I work with my dataset. The code is the same for both. I have no idea what I am missing.

Trt    Kg
 <chr> <dbl>
 CON    38.6
 CON    37.2
 CON    31.3
 CON    33.1
 CON    36.3
 TRT    34.8
 TRT    33.4
 TRT    33.5
 TRT    33.3
 

EXPF <- EXPBW %>%
  group_by (Trt) %>%
  summarize (mean_cw = mean(kg))

View(EXPF)

CodePudding user response:

As has been mentioned in the comments (@r2evans and @kybazzi), you have a typo (i.e., you use lower case kg when you have Kg in your dataframe) and you may need to be explicit with the dplyr package in your functions calls, as it may be trying to use summarize from the plyr package.

EXPF <- EXPBW %>%
  dplyr::group_by(Trt) %>%
  dplyr::summarize(mean_cw = mean(Kg))

Output

# A tibble: 2 × 2
  Trt   mean_cw
  <chr>   <dbl>
1 CON      35.3
2 TRT      33.8

Data

EXPBW <-
  structure(
    list(Trt = c("CON", "TRT"), mean_cw = c(35.3, 33.75)),
    class = c("tbl_df", "tbl", "data.frame"),
    row.names = c(NA,-2L)
  )

CodePudding user response:

I tried the code that you have provided, and it gives an error message saying that "object 'kg' not found".

You need to fix your code inside mean() from kg to Kg.

Then the code works fine.

  •  Tags:  
  • r
  • Related