Hello!

I already read a lot of posts in differents forums, but there was nothing that really helped me.

I use a dataset, which I imported with

data <- read_sav(file = "dataset.sav", user_na = TRUE)

I need some variables, which contain the information whether a person read a book or not. The data also contain values like "-2" (don't know), "-1" (no answer).., but I only need the information whether a person read a specific book or not. Therefore I recoded these factors to only "1" (person read the book) and "0" (person did not read the book).

I did this for 7 differents variables by using: (here are only shown two of them)

data$g2_neu <- car::recode(as.factor(data$g2), "-2=0; -1=0; 2=0")

data$g4_neu <- car::recode(as.factor(data$g4), "-2=0; -1=0; 2=0")

My aim is to count how many books have been read by one person. My dataframe consists of 4001 cases, so I want to build a new variable which is the sum of all the books a person read. I need this numeric variable to do a regression analysis afterwards.

To be able to count at all, I used as.numeric (because r doesn't count factors):

data$sum_g <- data$g2_neu + data$g4_neu

Now r counts every 0 als 1 (because the factor for all 0 is 1 in my dataframe) and every 1 as 2. But I want r to count the value 0 as 0 and 1 as 1.

What I already tried:

data$sum_g <- as.numeric(levels(data$g2_neu))[as.integer(data$g2_neu)] + as.numeric(levels(data$g4_neu))[as.integer(data$g4_neu)]

But that doesn't work, using this, r counts very strange things.

I also tried to recode the values, but that didn't work either:

data$g2_neu1 <- car::recode(as.numeric(data$g2_neu), "1=0; 2=1")

data$g4_neu1 <- car::recode(as.numeric(data$g4_neu), "1=0; 2=1")

I don't know what to do. Is it possible to count my value 0 (with the factor 1) as 0 and the 1 (which is the factor 2) as 1?

If anyone could help me, I would be very thankful!