Error mutating levels

Hi there,

I am trying to convert numerical responses (1:5) into likert-scale responses.

df <- read.csv("Testdata.csv")

   Q.06 Q.07 Q08 Q09 Q10
1    NA   NA  NA  NA  NA
2    NA   NA  NA  NA  NA
3    NA   NA  NA  NA  NA
4     5    5   2   1   1
5     4    4   2   2   1
6    NA   NA  NA  NA  NA
7     4    4   2   2   3
8     4    5   2   4   1
9     5    4   2   4   2
10    5    4   2   4   2

This is the code I am trying to use:

lbs <- c("strongly disagree", "disagree", "neutral",  "agree", "strongly agree")
survey <- df %>%
  dplyr::mutate_if(is.character, as.factor) %>%
  dplyr::mutate_if(is.numeric, as.factor, levels = 1:5, labels = lbs) %>%
  drop_na() %>%
  as.data.frame()

but I receive the following error message:

Error in mutate():
! Problem while computing Q.06 = (function (x) ....
Caused by error:
! unused arguments (levels = 1:5, labels = c("strongly disagree", "disagree", "neutral", "agree", "strongly agree"))

Any advice would be greatly appreciated.

Kind Regards

Aaron

Hi @aarontimo ,
This simplified version works:

suppressPackageStartupMessages(library(tidyverse))

df <- read.table(header=TRUE, text="
Q.06 Q.07 Q08 Q09 Q10
NA   NA  NA  NA  NA
NA   NA  NA  NA  NA
NA   NA  NA  NA  NA
 5    5   2   1   1
 4    4   2   2   1
NA   NA  NA  NA  NA
 4    4   2   2   3
 4    5   2   4   1
 5    4   2   4   2
 5    4   2   4   2
")

str(df)
#> 'data.frame':    10 obs. of  5 variables:
#>  $ Q.06: int  NA NA NA 5 4 NA 4 4 5 5
#>  $ Q.07: int  NA NA NA 5 4 NA 4 5 4 4
#>  $ Q08 : int  NA NA NA 2 2 NA 2 2 2 2
#>  $ Q09 : int  NA NA NA 1 2 NA 2 4 4 4
#>  $ Q10 : int  NA NA NA 1 1 NA 3 1 2 2

lbs <- c("strongly disagree", "disagree", "neutral",  "agree", "strongly agree")

survey <- df %>%
  dplyr::mutate_if(is.numeric, factor, levels = 1:5, labels = lbs) %>%
  drop_na() %>%
  as.data.frame()

str(survey)
#> 'data.frame':    6 obs. of  5 variables:
#>  $ Q.06: Factor w/ 5 levels "strongly disagree",..: 5 4 4 4 5 5
#>  $ Q.07: Factor w/ 5 levels "strongly disagree",..: 5 4 4 5 4 4
#>  $ Q08 : Factor w/ 5 levels "strongly disagree",..: 2 2 2 2 2 2
#>  $ Q09 : Factor w/ 5 levels "strongly disagree",..: 1 2 2 4 4 4
#>  $ Q10 : Factor w/ 5 levels "strongly disagree",..: 1 1 3 1 2 2
survey
#>             Q.06           Q.07      Q08               Q09               Q10
#> 1 strongly agree strongly agree disagree strongly disagree strongly disagree
#> 2          agree          agree disagree          disagree strongly disagree
#> 3          agree          agree disagree          disagree           neutral
#> 4          agree strongly agree disagree             agree strongly disagree
#> 5 strongly agree          agree disagree             agree          disagree
#> 6 strongly agree          agree disagree             agree          disagree

Created on 2022-09-16 with reprex v2.0.2

Thanks @DavoWW - that worked! Thanks again.

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.