Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable storing full variable names in attributes, and automatic use by likert function #7

wants to merge 6 commits into from
Show file tree
Hide file tree
Changes from all commits
File filter

Filter by extension

Filter by extension

Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ Collate:
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ S3method(print,
Expand Down
11 changes: 9 additions & 2 deletions R/likert.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,21 @@ likert <- function(items,
stop('All items (columns) must have the same number of levels')

# pull in long names from attributes, if exist
for (n in names(items)) {
if (!is.null(attr(items[[n]], "fullname"))) {
names(items)[names(items) == n] <- attr(items[[n]], "fullname")

lowrange <- 1 : ceiling(nlevels / 2 - nlevels %% 2)
highrange <- ceiling(nlevels / 2 + 1 ) : nlevels

results <- data.frame()
if(!is.null(grouping)) {
results <- data.frame(
Group = rep(unique(grouping), each=nlevels),
Response = rep(1:nlevels, length(unique(grouping)))
Group = rep(unique(na.omit(grouping)), each=nlevels),
Response = rep(1:nlevels, length(unique(na.omit(grouping))))
for(i in 1:ncol(items)) {
t <-, as.integer(items[,i])))
Expand Down
20 changes: 20 additions & 0 deletions R/likert.add.fullnames.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#'Takes a list of dataframe column names, and long names, and stores the long names in the attribute "fullname",
#'which can later be accessed by R.
#'db <- add_likert_fullnames(db, c(
#' "X7"="Do you use sites like Facebook, Twitter, or GPlus?",
# "X8"="Do you participate in online communities organised around your interests?",
#' "X10"="Do you know of online communities relevant to your discipline or the courses you are taking now?"))

likert_add_fullnames <- function(to, fnames) {

if(length(fnames) > length(unique(fnames))) {
stop("All names must be unique")

for(x in names(fnames)) {
attr(to[[x]], "fullname") <- fnames[[x]]
17 changes: 17 additions & 0 deletions R/
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#'Stores metadata about groups into a data frame, for future use by plot_likert_group.
#'Example usage (X7 etc are column names, titles are used as titles for group plots)
#'groups <- list(
#' "Use of social media"=c("X7","X8","X10","X11","X12"),
#' "Social learning"=c("X13","X14","X15","X16","X17","X18","X19","X20"),
#' "Connection with other students"=c("X21","X22","X23","X24","X25"),
#' "Interaction with professors"=c("X26","X27","X28","X29","X30","X31"),
#' "Experience of your major"='c("X32","X33","X34","X35","X36""),
#' "Identification with your discipline/major"=c("X40","X41","X42"))
#' db <- likert_store_groups(db, groups)

likert_store_groups <- function(db, groups) {
attr(db, "likert.groups") <- groups
25 changes: 25 additions & 0 deletions R/plot.likert.groups.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#'Plots a group of Likert-items, as stored by likert_store_groups. groups= can be one group, indexed by title or by number
#'or a list of groups. all=T prints all groups.

plot_likert_groups <- function(db, all=F, groups=NA, ...) {
attrgroups <- attr(db, "likert.groups")

if(is.null(attrgroups)) {
stop("You have not stored any groups using likert_store_groups")

if(all) {
groups <- names(attrgroups)

if(all( {
stop("You have not specified a group name using groups=, or all=F")

for(e in groups) {
group <- attrgroups[[e]]
ligroup <- likert(db[,group], ...)
print(plot(ligroup) + ggtitle(names(attrgroups[e])) )

38 changes: 38 additions & 0 deletions demo/FullNames.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@


# adding metadata to pisaitems, including a reading group, and full names
# this will be stored together with the pisaitems object, and can be used later
attr(pisaitems, "reading") <- c(

pisaitems <- add_likert_fullnames(pisaitems, c(
ST24Q01="I read only if I have to.",
ST24Q02="Reading is one of my favorite hobbies.",
ST24Q03="I like talking about books with other people.",
ST24Q04="I find it hard to finish books.",
ST24Q05="I feel happy if I receive a book as a present.",
ST24Q06="For me, reading is a waste of time.",
ST24Q07="I enjoy going to a bookstore or a library.",
ST24Q08="I read only to get information that I need.",
ST24Q09="I cannot sit still and read for more than a few minutes.",
ST24Q10="I like to express my opinions about books I have read.",
ST24Q11="I like to exchange books with my friends."))

# using the group reading to select the group of questions we want to plot
l24 <-pisaitems[,attr(pisaitems, "reading")]

# directly plotting, the full names have already been stored