0

From an online survey I gained a bunch of data. Some Items where answered with "Yes" or "No".

For my analysis I'd like to change the "Yes" into a 1 and the "No" into a 0.

How do I do that?

I tried the following: But that's obviously the wrong way around....

working_data$Schulabschluss_Mutter <- factor(working_data$F108, levels=c("Yes", "No",), labels=c(0,1))

I also tried this:

working_data$Schulabschluss_Mutte[working_data$F108 == "Yes"] <- 1

Didn't work either

Ric S
  • 9,073
  • 3
  • 25
  • 51
Linus
  • 41
  • 5
  • Hi and welcome to Stack Overflow! Could you provide some sample data you are working with and use the `dput` function on it, and then paste the results in your question? In this way people are facilitated in helping you. Thanks – Ric S Apr 02 '20 at 10:30
  • Hi Linus, welcome! Could [this](https://stackoverflow.com/questions/5824173/replace-a-value-in-a-data-frame-based-on-a-conditional-if-statement) help you? – R overflow Apr 02 '20 at 13:26

2 Answers2

0

Maybe with ifelse()?

working_data$Schulabschluss_Mutter <- ifelse(working_data$F108 == "Yes", 1, 0)

An example:

a <- c("Yes", "Yes", "No")
b <- c(1:3)

df <- data.frame(a,b)

df$c <- ifelse(ab$a == "Yes", 1, 0)

Output:

> df
    a b c
1 Yes 1 1
2 Yes 2 1
3  No 3 0
Cian
  • 58
  • 7
  • I was going to suggest `ifelse` myself but I would be careful in assuming that everything that is not *Yes* is *No* (what if you have *yes*?) I would explicitly handle Yes, No, "something else" or add something like `stopifnot(working_data$F108 %in% c('Yes', 'No'))` – dariober Apr 02 '20 at 10:40
  • That's a good point. And if some answers are already _1_ or _0_ it's a problem. I guess it depends on the data. – Cian Apr 02 '20 at 10:46
0

Quite many ways to skin this cat. Here are a few ideas.

Let's first generate some data:

structure(list(F108 = structure(c(2L, 2L, 1L, 1L), .Label = c("No",
"Yes"), class = "factor")), row.names = c(NA, -4L), class = "data.frame") -> df

In many cases, I find myself doing this:

df$Schulabschluss_Mutter <- 0
df$Schulabschluss_Mutter[df$F108 == 'Yes'] <- 1

This works well if you are sure that you only have Yes and No in your F108 column, but is not very elegant.

You could also create a new column and reset your factor levels, a la:

df$Schulabschluss_Mutter <- as.factor(df$F108)
levels(df$Schulabschluss_Mutter) <- c('0','1')

Or in dplyr:

library(tidyverse)
df %>% mutate(Schulabschluss_Mutter = recode(df$F108, `No`="0",`Yes`="1")) -> df

The latter are more robust if you have other options in addition to Yes / No.

Otto Kässi
  • 2,943
  • 1
  • 10
  • 27
  • 1
    That was very helpfull thanks! `df$Schulabschluss_Mutter <- 0 df$Schulabschluss_Mutter[df$F108 == 'Yes'] <- 1` That did the job! – Linus Apr 16 '20 at 13:56
  • Could I kindly ask you to approve my answer if my answer solved your issue? Glad to have helped :) – Otto Kässi Apr 17 '20 at 13:49