Reverse scale items

Hello community,
R beginner here. I'm trying to reverse scores in some columns in my scale.
Here's a repex:

data.frame(
       Item1 = c(3, 4, 4, 6, 7),
       Item2 = c(6, 7, 7, 8, 5),
       Item3 = c(5, 4, 3, 5, 7),
       Item4 = c(3, 8, 9, 8, 7),
       Item5 = c(4, 5, 6, 3, 2),
       Item6 = c(7, 8, 6, 5, 8)
)

In this situation, Items 1,3,5 should be reversed, ie: 3=7, 4=6, 6=4, 7=3.
I searched for answers, but almost all of them required downloading the "psych" or "car" packages, and for some reason I can't seem to be able to do that.

Is there a way to do such reversals simply? I have quite a few columns that require such reversals before I can average and analyze them as subscales.

Thanks!

Hi.

Bet you are running MAC OS (long:short--there's a temporary problem).

For the reverse functionality, it's surprisingly simple.

suppressPackageStartupMessages(library(dplyr)) 
x <- data.frame(
  Item1 = c(3, 4, 4, 6, 7),
  Item2 = c(6, 7, 7, 8, 5),
  Item3 = c(5, 4, 3, 5, 7),
  Item4 = c(3, 8, 9, 8, 7),
  Item5 = c(4, 5, 6, 3, 2),
  Item6 = c(7, 8, 6, 5, 8)
)

x[1]
#>   Item1
#> 1     3
#> 2     4
#> 3     4
#> 4     6
#> 5     7

x %>% mutate(Item1 = Item1[5:1])
#>   Item1 Item2 Item3 Item4 Item5 Item6
#> 1     7     6     5     3     4     7
#> 2     6     7     4     8     5     8
#> 3     4     7     3     9     6     6
#> 4     4     8     5     8     3     5
#> 5     3     5     7     7     2     8

Created on 2020-03-26 by the reprex package (v0.3.0)

I understood the reversal to mean flipping the scale of the values so that values that were 3 become 7, etc.

DF <- data.frame(
  Item1 = c(3, 4, 4, 6, 7),
  Item2 = c(6, 7, 7, 8, 5),
  Item3 = c(5, 4, 3, 5, 7),
  Item4 = c(3, 8, 9, 8, 7),
  Item5 = c(4, 5, 6, 3, 2),
  Item6 = c(7, 8, 6, 5, 8)
)
DF
#>   Item1 Item2 Item3 Item4 Item5 Item6
#> 1     3     6     5     3     4     7
#> 2     4     7     4     8     5     8
#> 3     4     7     3     9     6     6
#> 4     6     8     5     8     3     5
#> 5     7     5     7     7     2     8
Flip <- function(x) 10 - x
library(dplyr)
DF2 <- mutate_at(DF,.vars = c(1,3,5), Flip )
DF2
#>   Item1 Item2 Item3 Item4 Item5 Item6
#> 1     7     6     5     3     6     7
#> 2     6     7     6     8     5     8
#> 3     6     7     7     9     4     6
#> 4     4     8     5     8     7     5
#> 5     3     5     3     7     8     8

Created on 2020-03-26 by the reprex package (v0.3.0)

1 Like

Hi, yeah the code you gave is exactly what I'm looking for!
However, I tried it and it gave me this error:

> Flip <- function(x) 6 - x
> BII_rev <- mutate_at(average_BAOS_scores,.vars = c(106,107,108,110,111,112,118,119,120,121), Flip )
Error in 6 - x : non-numeric argument to binary operator
> BII_rev
Error: object 'BII_rev' not found

Do you know what went wrong?

My guess is that your columns are of the type character rather than numeric. What is the result of running

class(average_BAOS_scores[[106]])

If that comes back as numeric, try it with the other columns you tried to flip and see if any are character.

1 Like

All but one came back as numeric, but the funny thing is that col. 110, which came back as "character" is also numeric. Is there an easy way to define it as numeric?

I tried

average_BAOS_scores$BII_9 <- as.numeric(average_BAOS_scores$BII_9)

Since BII_9 is col110, but that didn't seem to do the trick

Was there any error or warning with

average_BAOS_scores$BII_9 <- as.numeric(average_BAOS_scores$BII_9)

After running that, what is the result of

is.numeric(average_BAOS_scores$BII_9

Okay, so it gives no error, and this is the output:

average_BAOS_scores$BII_9 <- as.numeric(average_BAOS_scores$BII_9)
> is.numeric(average_BAOS_scores$BII_9)
[1] TRUE

However when I check the class it still says it's character, and the reversing code doesn't work and gives the same error code as before

Are you sure column BII_9 is the problem? What happens if you do

BII_rev <- mutate_at(average_BAOS_scores,.vars = c(106,107,108,111,112,118,119,120,121), Flip )

skipping over column 110?

It doesn't give an error or a warning when I skip col110, but it also doesn't seem to have done anything in the table...

Are we still on the reprex data frame we started with

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(purrr)) 

othello <- function(col) {col = col[5:1]}


df_ <- data.frame(
  Item1 = c(3, 4, 4, 6, 7),
  Item2 = c(6, 7, 7, 8, 5),
  Item3 = c(5, 4, 3, 5, 7),
  Item4 = c(3, 8, 9, 8, 7),
  Item5 = c(4, 5, 6, 3, 2),
  Item6 = c(7, 8, 6, 5, 8)
)

map(df_,othello)
#> $Item1
#> [1] 7 6 4 4 3
#> 
#> $Item2
#> [1] 5 8 7 7 6
#> 
#> $Item3
#> [1] 7 5 3 4 5
#> 
#> $Item4
#> [1] 7 8 9 8 3
#> 
#> $Item5
#> [1] 2 3 6 5 4
#> 
#> $Item6
#> [1] 8 5 6 8 7

Created on 2020-03-26 by the reprex package (v0.3.0)

I started giving error codes that I got in my actual dataset, because it seems there's an issue with one of the columns.

It appears that the othello function doesn't quite do what I'd need it to do, it transformed some of the items correctly, but not all.

The Flip function seems right but like I said it's not working in my dataframe

We rely on the poster for representative data; there's nothing we can help with if the data given isn't actually representative of the data that proposed solutions are applied to.

You're right, I just didn't know how to create a dummy dataset that large, I'm sorry
It seems the previous issues with the psych package are resolved, I was able to download it,
but I'm now not too sure how to use it in a data frame as large as mine...
I have 193 columns total, and 82 rows of participants, I don't think I can define the

keys <- c(1, -1, -1, -1, 1, 1)

for all 193 columns, seeing as how there are some that not numeric. Is there any way I can make it easier for you or other users to help?

Found a solution finally!
This video: https://www.youtube.com/watch?v=60A3YxU2hCw&t=377s
Explains how to very simply recode select columns.

Downloading Rcmdr was tricky, but this site: https://www.andrewheiss.com/blog/2012/04/17/install-r-rstudio-r-commander-windows-osx/
Helped me tremendously, and I was able to get everything I needed done!

Huge thanks to everyone who replied, your help is greatly appreciated

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