R/Likert.R

Defines functions sortunique checkAllAnswers likert.rescale

Documented in checkAllAnswers likert.rescale sortunique

####################################################################################################.
####################################################################################################.
####################################################################################################.

sortunique <- function(x) {
  sort(unique(x))
}

# EXAMPLES
# # with numeric vector
# x <- c(1,2,3,4,5,6,7,8,9,4,3,2,5,4,1,1,2,3,4,7,8,7,NA,1,2,3,5,4,0,6,7,5,4,5,6)
# sortunique(x)
# # with characters
# x <- c('Sort', 'or', 'or', '(', ')','or', 'or', 'or', 'order', 'a', 'vector', 'or', 'factor', 'partially', 'into', 'ascending')
# sortunique(x)
# # with factors
# sortunique(ToothGrowth$supp)


####################################################################################################.
####################################################################################################.
####################################################################################################.
checkAllAnswers <- function(x) {
  min.r <- min(x, na.rm=T)
  max.r <- max(x, na.rm=T)

  d <- diff(sort(unique(x)))
  d.r <- ifelse(mean(d, na.rm=T) == 1, 'Yes', 'No')

  cat(
    paste0('value     :', sort(unique(x))),
    paste('range     : ', min.r, '-', max.r, sep = ''),
    paste('all values: ', d.r, sep = ''),
    sep = '\n',
    fill = T)
}

# EXAMPLES
# # vector with all values ranging from 0 to 9 (all values == Yes)
# x <- c(1,2,3,4,5,6,7,8,9,4,3,2,5,4,1,1,2,3,4,7,8,7,NA,1,2,3,5,4,0,6,7,5,4,5,6)
# checkAllAnswers(x)
# # vector with all values ranging from 0 to 9 (all values == No)
# y <- c(0,2,3,5,6,7,8,9,8,5,6,7,5,3,2,0,3,5,6,7,6,NA)
# checkAllAnswers(y)


####################################################################################################.
####################################################################################################.
####################################################################################################.
likert.rescale <- function(x, Val.min = NULL, Val.max = NULL) {
  if(is.null(Val.min) | is.null(Val.max))
  {
    x.new <- (x-min(x, na.rm=T))/(max(x, na.rm=T) - min(x, na.rm=T)) * 100

  } else {
    x.new <- (x-Val.min)/(Val.max - Val.min) * 100
  }

  checkAllAnswers(x)
  return(x.new)
}

# EXAMPLES
# x <- c(1,2,3,4,5,6,7,8,9,4,3,2,5,4,1,1,2,3,4,7,8,7,NA,1,2,3,5,4,0,6,7,5,4,5,6)
# x <- likert.rescale(x)
# x <- likert.rescale(x, 0,9)


####################################################################################################.
####################################################################################################.
####################################################################################################.
# likert.rev <- function(x, max) {
#   y <- (max+1)-x
#   return(y)
# }
# EXAMPLES
# x <- c(1,2,3,4,5,6,7,8,9,4,3,2,5,4,1,1,2,3,4,7,8,7,NA,1,2,3,5,4,0,6,7,5,4,5,6)
# likert.rev(x,9)


####################################################################################################.
####################################################################################################.
####################################################################################################.
# remove0 <- function(x) {
#   ifelse(x == 0, NA, x)
# }
# x <- c(0,2,3,4,5,6,7,8,9,4,3,2,5,4,1,1,2,3,4,7,8,7,NA,1,2,3,5,4,0,6,7,5,4,5,6)
# remove0(x)
alemiani/explora documentation built on May 28, 2019, 4:54 p.m.