--- title: 'Lecture 3: Importing and handling data II' author: "Dr Nicolaas Puts and Dr Lucas França" date: "17 October 2022" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` # Functions and pipe operators Functions in R can be difficult to read. For example. ```{r} a <- min(abs(count(x))) ``` Tidyverse packages introduced a novel approach to nest function: ```{r} library(tidyverse) ``` ```{r} a <- x %>% count() %>% abs() %>% min() ``` # Filtering data First we load a dataset in a similar way we did last time: ```{r} urlfile <- 'https://raw.githubusercontent.com/HeJasonL/BIG-TAC/main/TutorialData.csv' #this loads a csv file called TutorialData and puts it into urlfile data <- read.csv(urlfile) %>% as_tibble() ``` ```{r} data %>% filter(Sex == 'M') ``` # Merging data (and filter) First we need to create two datasets ```{r} sensory <- data %>% select(Sub_ID, GABA, Sensory1) ``` ```{r} IDs <- data %>% select(Sub_ID, Group, Age, Sex) ``` And we can merge those: ```{r} new_data <- inner_join(IDs, sensory) ``` # Summarising data and descriptives ```{r} summary(data) ``` We need to use factors in this case. ```{r} data$Group <- as.factor(data$Group) data$Sex <- as.factor(data$Sex) ``` ```{r} summary(data) ``` # The functions group_by() and summarise() ```{r} data %>% dplyr::group_by(Sex) %>% dplyr::summarise(mean_GABA = mean(GABA)) ``` ```{r} data %>% dplyr::group_by(Sex) %>% dplyr::summarise(mean_GABA = mean(GABA), sd_GABA = sd(GABA)) ``` # The function mutate() ```{r} data %>% dplyr::mutate(sensory_norm = Sensory1/34) ```