Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JoachimGoedhart/c341d616534ae2fab6ab73db423a8f76 to your computer and use it in GitHub Desktop.
Save JoachimGoedhart/c341d616534ae2fab6ab73db423a8f76 to your computer and use it in GitHub Desktop.
Reads non-tidy time-dependent data and calculates statistics
#Reads a non-tidy (wide) dataframe from csv file and assumes first column to be time, remaining columns measured parameter (Ratio) per condition (Cell).
df_wide <- read.csv("FRET-ratio-wide.csv", na.strings = "")
#Tidy the data, i.e. long format with each row is variable
df_tidy <- gather(df_wide, Cell, Ratio, -Time)
######### Calulcate summary statistics to fill dataframe 'df_summary' ########
# This is base R approach
df_summary <- data.frame(Time=df_wide$Time, n=tapply(df_tidy$Ratio, df_tidy$Time, length), mean=tapply(df_tidy$Ratio, df_tidy$Time, mean))
#Add SD and standard error of the mean to the dataframe
df_summary$sd <- tapply(df_tidy$Ratio, df_tidy$Time, sd)
df_summary$sem <- df_summary$sd/sqrt(df_summary$n-1)
#Add 95% CI of the mean to the dataframe
df_summary$CI_lower <- df_summary$mean + qt((1-Conf_level)/2, df=df_summary$n-1)*df_summary$sem
df_summary$CI_upper <- df_summary$mean - qt((1-Conf_level)/2, df=df_summary$n-1)*df_summary$sem
######### Calulcate summary statistics to fill dataframe 'df_summary' ########
# This is tidyverse approach
require(magrittr)
require(dplyr)
df_summary <- df_tidy %>%
group_by(Time) %>%
summarise(mean = mean(Ratio, na.rm = TRUE),
sd = sd(Ratio, na.rm = TRUE),
n = n()) %>%
mutate(sem = sd / sqrt(n - 1),
CI_lower = mean + qt((1-Conf_level)/2, n - 1) * sem,
CI_upper = mean - qt((1-Conf_level)/2, n - 1) * sem)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment