Just Another Statistics Textbook
  • Report an error
  • Request a clarification
  • Suggest Content

R Logic

  • Welcome
    • About
    • Installing R(Studio)
  • R Basics
    • Types of Scripts
    • R Fundamentals
    • R Logic
  • Statistics Foundations
    • Statistics Basics
    • Scientific Notation
    • Probability (R)
  • Describing Data
    • Central Tendency (R, Python, Excel, JASP)
    • Dispersion (R,Python, Excel, JASP)
  • Distributions
    • Binomial Distribution (R)
    • Normal Distribution
    • Skewness (R,Python)
    • Transforming Data (R)
  • Correlation
    • Correlations (R,Python)
    • Partial Correlations (R,Python)
  • Regressions
    • Simple regression (R)
    • Multiple Regressions (R)
  • General Linear Models
    • General Linear Models and Sum of Squares (R, Python)
    • T-Tests (R, Python incomplete)
    • One Way ANOVA (incomplete)
    • Repeated Measures ANOVAs
    • Mixed ANOVA (incomplete)
    • ANCOVA (incomplete)
  • Categorical
    • Contingency (R)
  • Item analyses
    • Cronbach’s Alpha (R,Python)
  • Multiple testing
    • Family-Wise Error (R)
    • False Discovery Rate(R)
    • FWER, FDR, Positive and Negative effects(R)
  • Permutations
    • Permutations (R)
    • Permutation vs. t tests (incomplete)
  • Excel tutorial
    • Formulas
    • If function
    • Count and countifs function
    • Sum and SumIf
    • Averageifs
    • Anchoring
  • Test yourself
    • All questions
    • Question Maker

On this page

  • Greater and less than using > and <
  • Comparing values using ==
  • Indexing/Selecting data
  • ! to reverse logic
  • And using &
  • OR using |

R Logic

Greater and less than using > and <

If you want to see if one object is larger than (>) or smaller (<) than another object, you can use the > and < operators:

1 > 2
[1] FALSE

Unsurprising that the above is false, as 1 is not greater than 2. Lets double check if 1 is less than 2:

1 < 2
[1] TRUE

Comparing values using ==

If you want to see if 2 objects are the same, then you can use ==. Lets check if 1 is the same as 2:

2 == 1
[1] FALSE

Unsurprisingly, 2 is not the same as 1. Lets see if 3/2 is the same as 1.5:

3/2 == 1.5
[1] TRUE

Great! What you’re more likely to want to do is to compare a vector to a value. So let’s imagine that you have asked your participants a question, and have a vector that identifies whether someone got an answer correct or not. Let’s compare that vector to the word “correct”:

correct_vector <- c("correct", "incorrect", "correct")
correct_vector == "correct"
[1]  TRUE FALSE  TRUE

This creates an logical vector of TRUE and FALSE values. Let’s use this now to select data:

Indexing/Selecting data

Sometimes you want to only focus on certain data, and indexing is a way to do this. We’re now going to create a data frame for a participant who has completed 3 trials of a reaction time task. This will include whether they were correct or not, and what their response time is. We will then using indexing to select the response times when the participant was correct:

response_table <- data.frame(
  accuracy = correct_vector, # see the vector created above
  response_times = c(100,105,180)
)
rmarkdown::paged_table(response_table)
# create an index using the logical "same as" operator
accuracy_index <- response_table$accuracy == "correct"

# use square brackets to use an index to select
accurate_trials_response_times <- response_table$response_times[accuracy_index]
# show the valid response times for accurate trials:
accurate_trials_response_times
[1] 100 180

Indexing is useful to remove unwanted data. In this case, most researchers think that response times when a participant makes an invalid response are not very informative, so they remove those response times using indexing above.

! to reverse logic

Sometimes you’ll want to flip the logic so that you get a FALSE when it would be TRUE, or TRUE when it would be FALSE. To do this, put in either a != instead of ==:

1 != 2
[1] TRUE

or a ! before the logical object or statement that you want to reverse:

correct_vector == "correct"
[1]  TRUE FALSE  TRUE
!correct_vector == "correct" 
[1] FALSE  TRUE FALSE
# which is the same as
!(correct_vector == "correct")
[1] FALSE  TRUE FALSE

And using &

If you want to get a TRUE outcome only if multiple statements are all TRUE, then you can use the “&” operator. Lets imagine you want to only focus on responses in your data that are correct AND quick enough i.e. less than 1000ms:

response_times_vector <- c (1200,600,800)
valid_responses <- response_times_vector < 1000 & correct_vector == "correct"
valid_responses
[1] FALSE FALSE  TRUE

So only the third response was both correct and quick enough.

OR using |

OR statements can be used to get a TRUE outcome if at least one of the logical statements is TRUE. Lets imagine that you were trying to select a subset of participants who either were colorblind or wore glasses. Your data might look like this:

eyesight_data <- data.frame(
  participant_number = c(1,2,3,4,5),
  eyesight           = c("colorblind","colorblind","uncorrected","uncorrected","glasses")
)
rmarkdown::paged_table(eyesight_data)

If we just wanted the rows that had people who were colorblind or wore glasess, we could create the following logical vector:

colorblind_glasses_vector <- eyesight_data$eyesight == "colorblind" | eyesight_data$eyesight == "glasses"
colorblind_glasses_vector
[1]  TRUE  TRUE FALSE FALSE  TRUE