![rstudio if statement rstudio if statement](https://community-cdn.rstudio.com/uploads/default/original/2X/c/c6d2dfecb18ce402fddc88a23b3835367836dda2.png)
The three main arguments to specify in pivot_wider() are Rather than incongruent, and congruent trials being represented down rows we are spreading them across columns (widening the data frame). So we will end up with is one row per subject and one column for each condition. In our example, what we want to do is pivot_wider() the mean RT values for the two conditions across different columns. In other words, it will spread values on different rows across different columns. The pivot_wider() function will convert a long data frame to a wide data frame. The tidyr package, like readr and dplyr, is from the tidyverse set of packages. To do so we will use the pivot_wider() function from the tidyr package. What we need to do is reshape the data frame.
![rstudio if statement rstudio if statement](https://ourcodingclub.github.io/assets/img/tutorials/etiquette/outline.png)
Currently, the mean RT for each condition is on a different row. It is easier to calculate the difference between two values when they are in the same row. Ultimately, we want to have one row per subject and to calculate the difference in mean RT between incongruent and congruent conditions. Let’s see examples of the two examples provided for ifelse() as a comparison.
RSTUDIO IF STATEMENT CODE
Anytime you need multiple ifelse() statements case_when() tends to simplify the code and logic involved. Know that you can place the additional ifelse() statement in either the TRUE or FALSE argument and can keep iterating on ifelse() statements for as long as you need (however that can get pretty complicated).Ĭase_when() is an alternative to an ifelse(). If accuracy is equal to 1, then if reaction time is less than or equal to 500, then set accuracy to 1. However, if the accuracy is 1, the value will depend on whether the reaction time is less than 500 (thus the second ifelse()). This makes sense because if the accuracy is 0 (incorrect), then the value needs to remain 0. The arguments for the first ifelse() are as follows: Accuracy is equal to 1.
RSTUDIO IF STATEMENT INSTALL
If you have not done so already, install the dplyr packageĭata % mutate( ACC = ifelse(ACC = 1, ifelse(RT <= 500, 1, 0), 0)) Summarise() aggregates across rows to create a summary statistic (means, standard deviations, etc.)įor more information on these functions Visit the dplyr webpage Group_by() splits data frame into separate groups based on specified columns
![rstudio if statement rstudio if statement](https://i.stack.imgur.com/iopud.png)
Mutate() creates new columns based on transformation from other columns, edits values within existing columns The core dplyr functions are:įilter() filters rows based on their values in specified columns As with any R function, you can think of functions in the dplyr package as verbs - that refer to performing a particular action on a data frame. The only time I end up needing a for loop is when importing a long list of files, or when creating code to put into a function.ĭplyr uses intuitive language that you are already familiar with. It can tempting to also think about writing for loops in your R script, but honestly for the most part for loops are avoidable thanks to a dplyr function called group_by(). Instead, hold the information in a new column within the data frame itself.įor example: A common strategy I see any many R scripts is to hold the mean or count of a column of values outside the dataframe and in a new variable in the Environment.ĭata <- ame( x = c( 1, 6, 4, 3, 7, 5, 8, 4), y = c( 2, 3, 2, 1, 4, 6, 4, 3)) data <- mutate(data, x_mean = mean(x), y_new = y - x_mean) head(data) # x y x_mean y_new It can be tempting to hold information outside of a data frame but in general I suggest avoiding this strategy. Not only is the language of dplyr intuitive but it allows you to perform data manipulations all within the dataframe itself, without having to create external variables, lists, for loops, etc. The language of dplyr will be the underlying framework for how you will think about manipulating a dataframe. It uses a Grammar of Data Manipulation that is intuitive and easy to learn.
RSTUDIO IF STATEMENT HOW TO
Now you will learn how to do stuff to that data frame using the dplyr package (which is of course part of the tidyverse)ĭplyr is one of the most useful packages in R. Last Chapter you learned how to import data files into R as dataframes. The most important object you will be using is the dataframe. In the Getting Started in R section you learned about the various types of objects in R. In this Chapter you will learn the fundamentals of data manipulation in R.