Help with a loop to replace variables

Hi. I'm working on a school project and i have this 5 point likert instrument which i have to change some variables because some variables represent "negative variables".
What i have to do is: if the questions's answer is 1, i gotta change it to 5, if its 2, i gotta change it to 4 and if its 3 it stays 3.

I have a big dataframe with all the data we collected, called merged_B. This is what i tried so far:

I made a vector with all the questions that i have to replace the values

negativas <- c(merged_B$`(adp18)`,merged_B$`(adp20)`,merged_B$`(adp22)`,merged_B$`(adp24)`, merged_B$`(adp26)`,merged_B$`(adp18)`,merged_B$`(aaa18)`,merged_B$`(aaa26)`,merged_B$`(aaa24)`,merged_B$`(aaa22)`,merged_B$`(aaa20)`,merged_B$`(atc10)`,merged_B$`(atc2)`,merged_B$`(atc4)`,merged_B$`(atc6)`,merged_B$`(atc8)`,merged_B$`(atm4)`,merged_B$`(atm8)`,merged_B$`(atm6)`,merged_B$`(atm10)`,merged_B$`(atm2)`,merged_B$`(cli18)`,merged_B$`(cli10)`,merged_B$`(cli2)`,merged_B$`(cli6)`,merged_B$`(cli12)`,merged_B$`(cli8)`,merged_B$`(cpt10)`,merged_B$`(cpt2)`,merged_B$`(cpt4)`,merged_B$`(cpt6)`, merged_B$`(cpt8)`,merged_B$`(crp10)`,merged_B$`(crp12)`,merged_B$`(crp18)`,merged_B$`(crp20)`,merged_B$`(crp4)`,merged_B$`(crp6)`,merged_B$`(crp8)`,merged_B$`(ctd2)`,merged_B$`(ctd4)`,merged_B$`(ctd6)`,merged_B$`(ctd8)`,merged_B$`(cmd10)`,merged_B$`(cmd2)`,merged_B$`(cmd6)`,merged_B$`(cmd4)`,merged_B$`(cmd8)`,merged_B$`(cmn4)`,merged_B$`(cmn6)`,merged_B$`(cmn8)`,merged_B$`(cmn2)`,merged_B$`(coo28)`,merged_B$`(coo8)`,merged_B$`(coo20)`,merged_B$`(coo26)`,merged_B$`(coo22)`,merged_B$`(coo23)`, merged_B$`(coo16)`,merged_B$`(flx10)`,merged_B$`(flx4)`,merged_B$`(ldc6)`,merged_B$`(ldc8)`,merged_B$`(ldc10)`,merged_B$`(ldc4)`,merged_B$`(ldc2)`,merged_B$`(mdc15)`,merged_B$`(mdc12)`,merged_B$`(mdc14)`,merged_B$`(mdc6)`,merged_B$`(mdc2)`,merged_B$`(mdc10)`,merged_B$`(mot10)`,merged_B$`(mot4)`,merged_B$`(mot2)`,merged_B$`(mot6)`,merged_B$`(mot8)`,merged_B$`(plc10)`,merged_B$`(plc2)`,merged_B$`(plc4)`,merged_B$`(plc6)`,merged_B$`(plc8)`,merged_B$`(rec8)`,merged_B$`(rec2)`,merged_B$`(rec12)`,merged_B$`(rec4)`,merged_B$`(rec14)`,merged_B$`(rec17)`,merged_B$`(rec10)`,merged_B$`(tol6)`,merged_B$`(tol4)`,merged_B$`(tol17)`,merged_B$`(tol10)`,merged_B$`(tol2)`,merged_B$`(tol8)`)

And then i tried to replace the variables like i said by doing

merged_B_negativas <- for (i in negativas){
merged_B[i== "1) Não tem nada a ver comigo"] <- "5" 
merged_B[i== "2) Tem pouco a ver comigo"] <- "4" 
merged_B[i== "3) As vezes tem e as vezes não tem a ver comigo"] <- "3" 
merged_B[i== "4) Tem muito a ver comigo"] <- "2" 
merged_B[i== "5) Tem tudo a ver comigo"] <- "1"
}

I could do it 1 by 1 but there are almost 100 questions, that would be a bummer.

I suffer a bit with loops so all help would be appreciated

I suggest doing it like this:

library(dplyr)

#Invent data
merged_B <- data.frame(AAA = c(5,3,2,1), `(adp18)` = c(3,2,5,1),
                       `(adp20)` = c(5,1,4,2), BBB = c(1,2,3,4), check.names = FALSE)
merged_B
#>   AAA (adp18) (adp20) BBB
#> 1   5       3       5   1
#> 2   3       2       1   2
#> 3   2       5       4   3
#> 4   1       1       2   4
##transform selected columns
COLS <- c("(adp20)", "(adp18)")
merged_B <- merged_B %>% mutate(across(.cols = all_of(COLS) ,.fns = ~ 6 - .))
merged_B
#>   AAA (adp18) (adp20) BBB
#> 1   5       3       1   1
#> 2   3       4       5   2
#> 3   2       1       2   3
#> 4   1       5       4   4

Created on 2021-09-08 by the reprex package (v0.3.0)

Looked promising but it gave me this

Erro: Problem with `mutate()` input `..1`.
x Can't subset columns that don't exist.
x Columns `5`, `5`, `5`, `5`, `5`, etc. don't exist.
i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.
Run `rlang::last_error()` to see where the error occurred.

Please show your code that generated the error.

Did it a few different ways .

First i saved a vector of colum names

negativas <- c("(adp18)","(adp20)","(adp22)","(adp24)","(adp26)","(adp18)","(aaa18)","(aaa26)","(aaa24)","(aaa22)","(aaa20)","(atc10)","(atc2)","(atc4)","(atc6)","(atc8)","(atm4)","(atm8)","(atm6)","(atm10)","(atm2)","(cli18)","(cli10)","(cli2)","(cli6)","(cli12)","(cli8)","(cpt10)","(cpt2)","(cpt4)","(cpt6)","(cpt8)","(crp10)","(crp12)","(crp18)","(crp20)","(crp4)","(crp6)","(crp8)","(ctd2)","(ctd4)","(ctd6)","(ctd8)","(cmd10)","(cmd2)","(cmd6)","(cmd4)","(cmd8)","(cmn4)","(cmn6)","(cmn8)","(cmn2)","(coo28)","(coo8)","(coo20)","(coo26)","(coo22)","(coo23)","(coo16)","(flx10)","(flx4)","(ldc6)","(ldc8)","(ldc10)","(ldc4)","(ldc2)","(mdc15)","(mdc12)","(mdc14)","(mdc6)","(mdc2)","(mdc10)","(mot10)","(mot4)","(mot2)","(mot6)","(mot8)","(plc10)","(plc2)","(plc4)","(plc6)","(plc8)","(rec8)","(rec2)","(rec12)","(rec4)","(rec14)","(rec17)","(rec10)","(tol6)","(tol4)","(tol17)","(tol10)","(tol2)","(tol8)")
merged_B <- merged_B %>% mutate(across(.cols = all_of(negativas),.fns = ~ 6 - .))

which resulted in

Erro: Problem with `mutate()` input `..1`.
x Can't subset columns that don't exist.
x Columns `(adp22)`, `(adp24)`, `(adp26)`, `(aaa18)`, `(aaa26)`, etc. don't exist.
i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.

Then i tried it identifying them on merged_B

negativas <- c(merged_B$`(adp18)`,merged_B$`(adp20)`,merged_B$`(adp22)`,merged_B$`(adp24)`, merged_B$`(adp26)`,merged_B$`(adp18)`,merged_B$`(aaa18)`,merged_B$`(aaa26)`,merged_B$`(aaa24)`,merged_B$`(aaa22)`,merged_B$`(aaa20)`,merged_B$`(atc10)`,merged_B$`(atc2)`,merged_B$`(atc4)`,merged_B$`(atc6)`,merged_B$`(atc8)`,merged_B$`(atm4)`,merged_B$`(atm8)`,merged_B$`(atm6)`,merged_B$`(atm10)`,merged_B$`(atm2)`,merged_B$`(cli18)`,merged_B$`(cli10)`,merged_B$`(cli2)`,merged_B$`(cli6)`,merged_B$`(cli12)`,merged_B$`(cli8)`,merged_B$`(cpt10)`,merged_B$`(cpt2)`,merged_B$`(cpt4)`,merged_B$`(cpt6)`, merged_B$`(cpt8)`,merged_B$`(crp10)`,merged_B$`(crp12)`,merged_B$`(crp18)`,merged_B$`(crp20)`,merged_B$`(crp4)`,merged_B$`(crp6)`,merged_B$`(crp8)`,merged_B$`(ctd2)`,merged_B$`(ctd4)`,merged_B$`(ctd6)`,merged_B$`(ctd8)`,merged_B$`(cmd10)`,merged_B$`(cmd2)`,merged_B$`(cmd6)`,merged_B$`(cmd4)`,merged_B$`(cmd8)`,merged_B$`(cmn4)`,merged_B$`(cmn6)`,merged_B$`(cmn8)`,merged_B$`(cmn2)`,merged_B$`(coo28)`,merged_B$`(coo8)`,merged_B$`(coo20)`,merged_B$`(coo26)`,merged_B$`(coo22)`,merged_B$`(coo23)`, merged_B$`(coo16)`,merged_B$`(flx10)`,merged_B$`(flx4)`,merged_B$`(ldc6)`,merged_B$`(ldc8)`,merged_B$`(ldc10)`,merged_B$`(ldc4)`,merged_B$`(ldc2)`,merged_B$`(mdc15)`,merged_B$`(mdc12)`,merged_B$`(mdc14)`,merged_B$`(mdc6)`,merged_B$`(mdc2)`,merged_B$`(mdc10)`,merged_B$`(mot10)`,merged_B$`(mot4)`,merged_B$`(mot2)`,merged_B$`(mot6)`,merged_B$`(mot8)`,merged_B$`(plc10)`,merged_B$`(plc2)`,merged_B$`(plc4)`,merged_B$`(plc6)`,merged_B$`(plc8)`,merged_B$`(rec8)`,merged_B$`(rec2)`,merged_B$`(rec12)`,merged_B$`(rec4)`,merged_B$`(rec14)`,merged_B$`(rec17)`,merged_B$`(rec10)`,merged_B$`(tol6)`,merged_B$`(tol4)`,merged_B$`(tol17)`,merged_B$`(tol10)`,merged_B$`(tol2)`,merged_B$`(tol8)`)
merged_B <- merged_B %>% mutate(across(.cols = all_of(negativas),.fns = ~ 6 - .))

which resulted in

Erro: Problem with `mutate()` input `..1`.
x Can't subset columns that don't exist.
x Locations 5, 5, and 5 don't exist.
i There are only 4 columns.
i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.

It makes no sense that "columns don't exist", i can find them at merged_B

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

I think i understand why it was telling me the variables didnt exist. A few variables had an extra space after the last parenthesis. I fixed it now. Still got one question, how can i use

merged_B <- merged_B %>% mutate(across(.cols = all_of(negativas),.fns = ~ 6 - .))

only if the variable isnt NA, but without removing the NA's from the dataframe?

I do not understand what you want to accomplish with your last question. The calculation 6 - . will return NA for the elements that are NA. If a column has the values 5,3, NA, 1, it will be changed to 1, 3, NA, 5. What do you want to get for the result?

Now that i fixed those variables so they can be identified, i've been getting this error when i run that code:

Erro: Problem with `mutate()` input `..1`.
x argumento não-numérico para operador binário
i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`

which translates to non-numeric argument to binary operator.

That is probably caused by one or more of your columns not being a numeric column. That can be caused by characters used to mark missing values. You can check the class of your columns with

str(merged_B)

If you have many columns and the output of str() is hard to read, you can find the numeric position of non-numeric columns with

CLASSES <- unlist(lapply(merged_B, class))
which(CLASSES != "numeric")

Yeah, basically all my variables happen to be character. Can i make all my columns except c(1,2,3,4,5,6,7,8,12) as numeric ? when i try i get Erro: 'list' object cannot be coerced to type 'double'

Try

merged_B <- merged_B %>% mutate(across(.cols = -c(1:8,12), .fns = as.numeric))

You will probably get a message or warning the "NAs were produced by coercion", meaning that the characters that marked NA elements could not be converted to numbers.

Everything is working now. Thanks for sticking with me for so long :smiley:

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.