Reputation: 1
This is my code:*
data %>%
mutate(r_stemster_3 = recode(stemster_3,
`1` = 7,
`2` = 6,
`3` = 5,
`4` = 4,
`5` = 3,
`6` = 2,
`7` = 1))
This is the error message:
Error in `mutate()`:
ℹ In argument: `r_stemster_3 = recode(...)`.
Caused by error in `recode()`:
! unused arguments (`1` = 7, `2` = 6, `3` = 5, `4` = 4, `5` = 3, `6` = 2, `7` = 1)
Backtrace:
1. data %>% ...
3. dplyr:::mutate.data.frame(...)
4. dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
6. dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
7. mask$eval_all_mutate(quo)
8. dplyr (local) eval()
Error in mutate(., r_stemster_3 = recode(stemster_3, `1` = 7, `2` = 6, :
Caused by error in `recode()`:
! unused arguments (`1` = 7, `2` = 6, `3` = 5, `4` = 4, `5` = 3, `6` = 2, `7` = 1)
I can't figure out what was wrong with my code. Please help fix the code!
I tried variations of the code such as removing the backticks or using single/double quotes instead of backticks. None of them worked.
Also data$stemster_3 is numeric and I want the reverse coded variable to be numeric too.
Upvotes: 0
Views: 100
Reputation: 171
There are many ways to deal with reverse coding in R. Here are some:
sampledf <- data.frame(V1 = c(1, 4, 5, 6, 7),
V2 = c(2, 4, 4, 5, 6),
V3 = c(2, 7, 1, 1, 5))
8 - sampledf
sampledf <- data.frame(V1 = c(1, 4, 5, 6, 7),
V2 = c(2, 4, 4, 5, 6),
V3 = c(2, 7, 1, 1, 5))
install.packages("psych")
library(psych)
reverseitems <- c(-1, 1, 1)
reverse.code(reverseitems, sampledf, mini = 1, maxi = 7)
note. reverseitems <- c(-1, 1, 1)
is here to indicate V1
in the dataset. In this case, only the values in the first column will be reverse-coded.
sampledf <- data.frame(V1 = c(1, 4, 5, 6, 7),
V2 = c(2, 4, 4, 5, 6),
V3 = c(2, 7, 1, 1, 5))
library(dplyr)
sampledf |>
mutate(new = 8 - sampledf)
note. In this way, new variables will be added to the dataset (sampledf
).
apply()
functionsampledf <- data.frame(V1 = c(1, 4, 5, 6, 7),
V2 = c(2, 4, 4, 5, 6),
V3 = c(2, 7, 1, 1, 5))
sample123 <- apply(sampledf[, c(1, 2, 3)], 2, function(x) 8-x)
sample123
note. By using c(1, 2, 3)
you can specify the variables (eg. all variables). After having new dataset, you can merge the specific variables of the old and new datasets (data.frame()
function).
Upvotes: 1