I have a dataset with 49 columns. 

```
'data.frame':   1351 obs. of  47 variables:
 $ ID    : Factor w/ 1351 levels "PID0001","PID0002",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ Survey: int  1 2 1 1 2 2 2 1 1 2 ...
 $ hsinc1: int  2 4 4 4 5 4 3 3 1 1 ...
 $ hsinc2: int  2 3 3 3 4 3 3 3 1 1 ...
 $ hsinc3: int  4 4 2 3 3 4 5 4 5 5 ...
 $ hsinc4: int  4 4 4 4 4 4 4 4 5 4 ...
 $ hfair1: int  2 2 2 1 1 1 1 2 1 2 ...
 $ hfair2: int  4 5 5 4 5 5 5 5 5 5 ...
 $ hfair3: int  4 5 4 3 5 4 3 3 5 5 ...

```

*etc ...*

---

I want to reverse code columns `5,6,8,9,10,12,13,14,17` and `18` such that a score of `5` becomes a score of `1`, and `4` becomes `2` etc.

At first, I thought this was achievable by using the `psych::reverse.code()` function, so I tried this: 

With the `-1`'s being the `5,6,8,9,10,12,13,14,17` and `18` columns.

```
library('psych')
keys <-c(1,1,1,1,-1,-1,1,-1,-1,-1,1,-1,-1,-1,1,1,-1,-1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)

df_rev <- reverse.code(keys, items = df, mini = rep(1,49), maxi = rep(5,49))

```

However, when I run this code, I get the following error:

```
Error in items %*% keys.d :
requires numeric/complex matrix/vector arguments

```

**Can anybody help with this, please?**

---

Another method I have just been trying is to create a subset of the original data frame, with just the columns I want to reverse code:

```
data_to_rev <- df[c(5,6,8,9,10,12,13,14,17,18)]

```

And then reverse coding this subset:

```
keys <- c(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1)
df_rev <- reverse.code(keys, items = data_to_rev, mini = rep(1,10), maxi = rep(5,10))

```

This works successfully. All variables are now reverse coded like I need them. However, **how do I get this subset of reverse coded values and place it back into the original data frame - overwriting the old (non-reversed) columns?**

---

Any help would be hugely appreciated, thank you!

---

**EDIT - SOLUTION**

I think I have managed to solve it using @MikeH's help.

I created a subset of just the participant ID's (the factor variable) `data_ID <- df[1]`

And then used:

`data_rev <- reverse.code(keys, items = df[,-1], mini = rep(1,46), maxi = rep(5,46))`

This leaves me with 2 data frames/subsets:

* 1 with all the participant ID's.
* 1 with all their data and columns `5,6,8,9,10,12,13,14,17` and `18` reverse coded.

I then used: `data_final <- cbind(data_ID, data_rev)` to join the 2 subsets back together. 

Can anyone see anything wrong with this method? I think it has worked upon visual inspection...