简体   繁体   中英

How do I take certain values from a dataframe, average them, and place them in a new dataframe in r?

I have a data frame that contains two columns. Column V1 contained coordinates for a 96 well plate ie, A1, A2, A3, and so on until H12. Column V2 contains integer values for each cell. Say cell A6 and A12 are values for a certain treatment in the plate. How would I average these values and place them in a new data frame with the first column being the name of the treatment, the second being the average, and the third being the standard deviation?

In the end, I would like a bar graph with error bars. I have many plates so it became too cumbersome to make in excel. This is how they look in excel

Here is a small example dataframe

data <- data.frame(Cell = c("A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11", "A12"),
                   Value = c(4, 6, 2, 7, 4, 6, 1, 6, 2, 3, 4, 6))

Say cells A6 and A12 represent treatment A. I would like a new data frame like this:

result <- data.frame(Treatment = c("A", "B", "C", "D", "E", "F"),
                   Mean = c(6, 6, 2, 7, 4, 6),
                   SD = c(0, 2, 1.5, .75, 5, 2))

Using tidyverse library to subset cells containing "6" and "12", and take the first letter to create a new grouping variable for treatment:

library(tidyverse)

data <- data %>% 
  filter(grepl("6", Cell) | grepl("12", Cell)) %>% 
  mutate(treatment = substr(Cell, 1, 1)) 

  Cell Value treatment
1   A6     6         A
2  A12     6         A

Summarise by group:

data %>% 
  group_by(treatment) %>%
  summarise(mean = mean(Value), sd = sd(Value))

# A tibble: 1 x 3
    treatment  mean    sd
    <chr>     <dbl> <dbl>
  1 A             6     0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM