Alina Dzaferovic
Alina Dzaferovic

Reputation: 11

Recoding of item files with if-loop in R

I´m a uni student who recently started learning to use statistical analysis code with R. Unfortunately I´m not the best yet, and my prof. isn´t really willing to help. Which is why I´m hoping that maybe one of you knows the answer to my question: What did I do wrong? My goal was to recode the Items the other way around, so that 1=5, 2=4, ... I thought that with an if-loop I´d be able to recode the items, but the script won't run.

For background, we did a survey that featured an experimental manipulation and included questions, that were both poled in a positive and negative direction. To add the individual questions together in an index (so that I can operationalize the construct), I thought to pole them first in the same direction.

This is the code I came up with. mig stands for the dataset. The #comments are in German and simply are for structural reasons. Also the t-test doesn't work .

I would be forever grateful if anybody knew how I could improve the code so it runs. :) Thank you all in advance, I appreciate it!!!

library(tidyverse)
library(report)
#Datensatz einlesen:
mig <- readxl::read_excel("data/data_ExperimentMigration2024_2024-07-04_20-34.xlsx")

#versuch Items umzupolen:
mig |>
  mutate(ME03_02_rec = if_else(ME03_02 == 1 | ME03_02 == 2 | ME03_02 ==3 | ME03_02 ==4 | ME03_02 == 5, "1=5", "2=4", "3=3", "4=2", "5=1"))

#Items umpolen
mig |> #Asylsuchende nehmen Deutschen die Arbeitsplätze weg.
  mutate(
    ME03_02 = if_else(ME03_02 == 1, 1=5, NA),
    ME03_02 = if_else(ME03_02 == 2, 2=4, NA),
    ME03_02 = if_else(ME03_02 == 3, 3=3, NA),
    ME03_02 = if_else(ME03_02 == 4, 4=2, NA),
    ME03_02 = if_else(ME03_02 == 5, 5=1, NA)
    )
mig |> #Zuwanderung verschlimmert die Wohnungsknappheit.
  mutate( ME03_01 = if_else(ME03_01 == 1, 1=5, NA),
          ME03_01 = if_else(ME03_01 == 2, 2=4, NA),
          ME03_01 = if_else(ME03_01 == 3, 3=3, NA),
          ME03_01 = if_else(ME03_01 == 4, 4=2, NA),
          ME03_01 = if_else(ME03_01 == 5, 5=1, NA)
  )
mig |> #Flucht vor Armut ist weniger legitim als Flucht vor Krieg oder aus politischen Gründen.
  mutate( ME03_04 = if_else(ME03_04 == 1, 1=5, NA),
          ME03_04 = if_else(ME03_04 == 2, 2=4, NA),
          ME03_04 = if_else(ME03_04 == 3, 3=3, NA),
          ME03_04 = if_else(ME03_04 == 4, 4=2, NA),
          ME03_04 = if_else(ME03_04 == 5, 5=1, NA)
  )
mig |> #Zuwanderung wirkt sich negativ auf die Wirtschaft aus.
  mutate( ME03_09 = if_else(ME03_09 == 1, 1=5, NA),
          ME03_09 = if_else(ME03_09== 2, 2=4, NA),
          ME03_09 = if_else(ME03_09 == 3, 3=3, NA),
          ME03_09 = if_else(ME03_09 == 4, 4=2, NA),
          ME03_09 = if_else(ME03_09 == 5, 5=1, NA)
  )

#Items Umpolen?
mig <- mig |>
  mutate(
    ME03_02 = recode(ME03_02, `1` = 5, `2` = 4, `3` = 3, `4` = 2, `5` = 1),
    ME03_01 = recode(ME03_01, `1` = 5, `2` = 4, `3` = 3, `4` = 2, `5` = 1),
    ME03_04 = recode(ME03_04, `1` = 5, `2` = 4, `3` = 3, `4` = 2, `5` = 1),
    ME03_09 = recode(ME03_09, `1` = 5, `2` = 4, `3` = 3, `4` = 2, `5` = 1)
  )

#Index Bilden
einst_index <- mig |>
  select(ME03_01, ME03_02, ME03_03, ME03_04, ME03_05, ME03_07, ME03_09, ME03_10) |>
  rowMeans(na.rm = TRUE)
#index zum datensatz hinzufügen:
mig<- mig
  mutate(einst_index = einst_index)
#Mittelwert&sd
mig |>
  summarise(
    mean(einst_index, na.rm = TRUE), sd(einst_index, na.rm=TRUE)
  )

#T-Test für unabhängige Stichproben
# Einst_index x neutraler Stimulus:
t.test(mig$einst_index, mig$EM03, paired = TRUE) |>
  report_table()
#Einst_index x positiver Stimulus:
t.test(mig$einst_index, mig$EM02, paired = TRUE) |>
  report_table()
#Einstellung_index x negativer Stimulus:
t.test(mig$einst_index, mig$EM01, paired = TRUE) |>
  report_table()

Upvotes: 0

Views: 59

Answers (1)

Claudio
Claudio

Reputation: 1528

You want to recode you original variable so that 5 = 1, 4 = 2 etc. Your new variable is a function of the old variable, so you don't need a loop or an if_else statement, you just need to compute the new value:

old <- 1:5
new = old*-1 + 6
cbind(old, new)
#>      old new
#> [1,]   1   5
#> [2,]   2   4
#> [3,]   3   3
#> [4,]   4   2
#> [5,]   5   1

Created on 2024-07-08 with reprex v2.1.0

Please note that your if_else statement is wrong, the second argument should be the value you want to assign to the target variable when the condition (first argument) is TRUE, not an assignment, in fact it throws an error:

library(dplyr)
old <- 1:5
new = if_else(old == 1, 1 = 5, NA)
#> Error: <text>:3:27: unexpected '='
#> 2: old <- 1:5
#> 3: new = if_else(old == 1, 1 =
#>                              ^

Created on 2024-07-08 with reprex v2.1.0

That's fortunate, though, because if it didn't you would end up with NA's for all values that are not 1: that's what you're asking for in the second argument.

If you want to stick with an if_else-like approach, you'd better take a look at the case_when() function, that is better suited when you have multiple conditions:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
df <- tibble(old = 1:5)
df <-  mutate(df, new = case_when(
  old == 1 ~ 5,
  old == 2 ~ 4,
  old == 3 ~ 3,
  old == 4 ~ 2,
  old == 5 ~ 1))
print(df)
#> # A tibble: 5 × 2
#>     old   new
#>   <int> <dbl>
#> 1     1     5
#> 2     2     4
#> 3     3     3
#> 4     4     2
#> 5     5     1

Created on 2024-07-08 with reprex v2.1.0

Upvotes: 1

Related Questions