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()
Alina Dzaferovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.