jeudi 30 avril 2020

Given an imported dataset, how do I assign the value of 0 to a new column if a value of a previous column = 1? On R

I'm a beginner to R so please excuse me if this is an overly simple question.

With n-back data, I'm trying to calculate the bias of the one-back trials.

I imported a dataset from Excel to R successfully and everything works except for the line

if (df$one_back_aprime == 1) {
  df$one_back_bias <- 0
}

in the following code:

library(readxl)
nback_mornings_data <- read_excel("NFS4and5_mornings_R.xlsx", sheet = 1) #replace with file name
df <- data.frame(nback_mornings_data)

if (df$one_back_hit_rate > df$one_back_fa_rate) {
  df$one_back_aprime <- 0.5 + ((df$one_back_hit_rate - df$one_back_fa_rate) * (1 + df$one_back_hit_rate - df$one_back_fa_rate))/(4 * df$one_back_hit_rate * (1 - df$one_back_fa_rate))
  } else if (df$one_back_hit_rate < df$one_back_fa_rate) {
    df$one_back_aprime <- 0.5 + ((df$one_back_fa_rate - df$one_back_hit_rate) * (1 + df$one_back_fa_rate - df$one_back_hit_rate))/(4 * df$one_back_fa_rate * (1 - df$one_back_hit_rate))
  } 

if (df$one_back_aprime == 1) {
  df$one_back_bias <- 0
} else {
  df$one_back_bias <- (((1 - df$one_back_hit_rate) * (1 - df$one_back_fa_rate)) - (df$one_back_hit_rate * df$one_back_fa_rate)) / (((1 - df$one_back_hit_rate) * (1 - df$one_back_fa_rate)) + (df$one_back_hit_rate * df$one_back_fa_rate))
}

When I run the code, everything works, except when the one_back_aprime == 1 , it prints NaN (because that's what the equation at else churns out). However, I'm confused as to why this is because I already put that it should be assigned the value of 0. So clearly I'm doing that wrong.
Can anyone please help to change that portion of the code so that when one_back_aprime = 1, the new one_back_bias column will show the value of 0? Note: I've tried print("0") but that doesn't work either.

Any help would be much appreciated! I'm sure I'm missing something very basic, but again, I'm just a beginner.

Aucun commentaire:

Enregistrer un commentaire