0

I have one ordinal variable which I like to become dichotomous, see code below and example dataset here. What I like to have is that the labels c(1:10) are regrouped as followed:

  • 0 <- c("insignificant", "2", "3")
  • removed c("4", "5", "6", "7")
  • 1 <- c("8", "9", "very relevant")

How can I achieve that?

Code:

library(ggplot2) # professional plots
library(reshape2) # professional boxplots
library(plyr) # to rename
library(likert) # likert scales
library(gridExtra) # for arrangeGrob() to write footnotes in plots
library(stringr) # for wrapping labels via str_wrap in plots
competence_bachelor <- rawdata[, substr(names(rawdata), 1, 4) == "Q002"]
competence_bachelor <- rename(competence_bachelor, c(Q002_01 = "Ability to propose new ideas and new solutions", Q002_02 = "Ability to present in public", Q002_03 = "Ability to use a computer", Q002_04 = "Ability to use the Internet", Q002_05 = "Ability to use statistical programs", Q002_06 = "Ability to write reports", Q002_07 = "Knowledge of economic concepts", Q002_08 = "Knowledge of legal concepts", Q002_09 = "Ability to understand the other's point of view", Q002_10 = "Ability to rapidly acquire new knowledge", Q002_11 = "Ability to team work", Q002_12 = "Ability to do analysis with quantitative methods", Q002_13 = "Ability to do analysis with qualitative methods", Q002_14 = "Knowledge of English", Q002_15 = "Knowledge of another foreign language"))
i <- 1
while(i <= ncol(competence_bachelor)) {
  competence_bachelor[[i]] = factor(competence_bachelor[[i]],labels = c("insignificant", "2", "3", "4", "5", "6", "7", "8", "9", "very relevant"), levels = c(1:10))
i <- i + 1
}
competence_bachelor_plot <- likert(competence_bachelor)
p <- plot(competence_bachelor_plot, centered = FALSE, include.histogram = FALSE) + ggtitle("How do you rate your skills gained with the Bachelor's?*") + theme(axis.text.y = element_text(colour = "black"), axis.text.x = element_text(colour = "black")) + theme(legend.key.size = unit(0.02,"npc"))+guides(fill = guide_legend(""))
g <- arrangeGrob(p, sub = textGrob("*Order of questions was randomized and only extremes labeled in online questionaire.", x = 0, hjust = -0.1, vjust = 0.1, gp = gpar(fontface = "italic", fontsize = 10)))
print(p)
ggsave((filename = "competence_bachelor.pdf"), scale = 1, width = par("din")[1], height = par("din")[2], units = c("in", "cm", "mm"), dpi = 300, limitsize = TRUE, g)

Edited version of the source code:

Which basically adds the following line to the original one: levels(competence_bachelor_dich) <- list("0" = c("insignificant", "2", "3"), "1" = c("8", "9", "very relevant")) # set levels

library(ggplot2) # professional plots
library(reshape2) # professional boxplots
library(plyr) # to rename
library(likert) # likert scales
library(gridExtra) # for arrangeGrob() to write footnotes in plots
library(stringr) # for wrapping labels via str_wrap in plots
competence_bachelor_dich <- rawdata[, substr(names(rawdata), 1, 4) == "Q002"] # dichotomous variable
competence_bachelor_dich <- rename(competence_bachelor_dich, c(Q002_01 = "Ability to propose new ideas and new solutions", Q002_02 = "Ability to present in public", Q002_03 = "Ability to use a computer", Q002_04 = "Ability to use the Internet", Q002_05 = "Ability to use statistical programs", Q002_06 = "Ability to write reports", Q002_07 = "Knowledge of economic concepts", Q002_08 = "Knowledge of legal concepts", Q002_09 = "Ability to understand the other's point of view", Q002_10 = "Ability to rapidly acquire new knowledge", Q002_11 = "Ability to team work", Q002_12 = "Ability to do analysis with quantitative methods", Q002_13 = "Ability to do analysis with qualitative methods", Q002_14 = "Knowledge of English", Q002_15 = "Knowledge of another foreign language"))
i <- 1
while(i <= ncol(competence_bachelor_dich)) {
  competence_bachelor_dich[[i]] = factor(competence_bachelor_dich[[i]],labels = c("insignificant", "2", "3", "4", "5", "6", "7", "8", "9", "very relevant"), levels = c(1:10))
  levels(competence_bachelor_dich) <- list("0" = c("insignificant", "2", "3"), "1" = c("8", "9", "very relevant")) # set levels
  i <- i + 1
}
competence_bachelor_dich_plot <- likert(competence_bachelor_dich)
p <- plot(competence_bachelor_dich_plot, centered = FALSE, include.histogram = FALSE) + ggtitle("How do you rate your skills gained with the Bachelor's?*") + theme(axis.text.y = element_text(colour = "black"), axis.text.x = element_text(colour = "black")) + theme(legend.key.size = unit(0.02,"npc"))+guides(fill = guide_legend(""))
g <- arrangeGrob(p, sub = textGrob("*Order of questions was randomized and only extremes labeled in online questionaire.", x = 0, hjust = -0.1, vjust = 0.1, gp = gpar(fontface = "italic", fontsize = 10)))
print(p)
ggsave((filename = "competence_bachelor_dich.pdf"), scale = 1, width = par("din")[1], height = par("din")[2], units = c("in", "cm", "mm"), dpi = 300, limitsize = TRUE, g)
Til Hund
  • 1,543
  • 5
  • 21
  • 37

1 Answers1

1

One possibility is to convert your variable to a factor, and set the levels attribute as a named list, specifying how to rename/regroup the levels.

# a minimal version of your variable
x <- c("insignificant", "2", "3", "4", "5", "6", "7", "8", "9", "very relevant")

# convert to factor
x2 <- as.factor(x)

# set levels
levels(x2) <-list("0" = c("insignificant", "2", "3"),
                  "1" = c("8", "9", "very relevant"))
# [1] 0    0    0    <NA> <NA> <NA> <NA> 1    1    1 
Henrik
  • 65,555
  • 14
  • 143
  • 159
  • Thank you, Henrik. How can I plot this in ggplot2 then? I received the following error: Error en data.frame(x = c(10L, 10L, 10L, 10L, 10L, 9L, 7L, 10L, 10L, 8L, : arguments imply differing number of rows: 611, 43 – Til Hund Apr 01 '14 at 12:58
  • Your question (which I answered here) was about converting an ordinal variable to a dichotomous. I think it is better that you post a new question about your plotting problems. Please read [**here**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) on how to make an easily copy-paste-able, reproducible example. Cheers. – Henrik Apr 01 '14 at 13:03
  • I am still struggling to implement your minimal version in the source code, see second version of my source code which does not work out, but does not give an error message either. Would you be so kind as to insert your code snippet into the published code above, please? This would help me a lot to understand. – Til Hund Apr 30 '14 at 10:04