 # How to build sums of factor-columns as.numeric?

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.

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! ``````
#a '0' and a '1' would like to be able to sum these two as numeric and get result 1
(a_factor_vec <- factor(x=c("0","1")))

(a_factor_vec_as_int <- as.integer(a_factor_vec))

(a_factor_vec_as_int_min_1 <- a_factor_vec_as_int -1 )

sum(a_factor_vec_as_int_min_1)``````

theres an equal sign in my code only because I am making a factor out of nothing, so I need to make it equal to what it is. two items '0' and '1'
you already have whatever factors you have, so you don't need that step ( because you implicitly have it)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Thanks! The following error occurs:

Fehler: Unerwartete(s) '=' in "(a_factor_vec <- factor((data\$g2_neu)="

(Unexpected '=')

for all we know thats the correct sum...
If you want more detailed help, you could attempt creating a reprex. I attach a guide
FAQ: How to do a minimal reproducible example ( reprex ) for beginners

Ah okay, right! I am sorry! Thanks you Now it worked, but the result for my sum is 1111