Difference between revisions of "OpasnetUtils/Drafts"

From Testiwiki
Jump to: navigation, search
(Answer: ova2spat updated)
(Answer: webropol.convert and merge.questions were moved from Goherr: Fish consumption study)
Line 18: Line 18:
  
 
library(OpasnetUtils)
 
library(OpasnetUtils)
 +
 +
############## Generic functions and objects are defined first.
 +
 +
### webropol.convert converts a csv file from Webropol into a useful data.frame.
 +
 +
webropol.convert <- function(
 +
  data, # Data.frame created from a Webropol csv file. The first row should contain headings.
 +
  rowfact, # Row number where the factor levels start (in practice, last row + 3)
 +
  textmark = "Other open" # The text that is shown in the heading if there is an open sub-question.
 +
) {
 +
  out <- dropall(data[2:(rowfact - 3) , ])
 +
  subquestion <- t(data[1 , ])
 +
  subquestion <- gsub("\xa0", " ", subquestion)
 +
  subquestion <- gsub("\xb4", " ", subquestion)
 +
  subquestion <- gsub("\n", " ", subquestion)
 +
  #  subquestion <- gsub("\\(", " ", subquestion)
 +
  #  subquestion <- gsub("\\)", " ", subquestion)
 +
  textfield <- regexpr(textmark, subquestion) != -1
 +
  subquestion <- strsplit(subquestion, ":") # Divide the heading into a main question and a subquestion.
 +
  subqtest <- 0 # The previous question name.
 +
  for(i in 1:ncol(out)) {
 +
    #print(i)
 +
    if(subquestion[[i]][1] != subqtest) { # If part of previous question, use previous fact.
 +
      fact <- as.character(data[rowfact:nrow(data) , i]) # Create factor levels from the end of Webropol file.
 +
      fact <- fact[fact != ""] # Remove empty rows
 +
      fact <- gsub("\xa0", " ", fact)
 +
      fact <- gsub("\xb4", " ", fact)
 +
      fact <- gsub("\n", " ", fact)
 +
      fact <- strsplit(fact, " = ") # Separate value (level) and interpretation (label)
 +
    }
 +
    if(length(fact) != 0 & !textfield[i]) { # Do this only if the column is not a text type column.
 +
      out[[i]] <- factor(
 +
        out[[i]],
 +
        levels = unlist(lapply(fact, function(x) x[1])),
 +
        labels = unlist(lapply(fact, function(x) x[2])),
 +
        ordered = TRUE
 +
      )
 +
    }
 +
    subqtest <- subquestion[[i]][1]
 +
  }
 +
  return(out)
 +
}
 +
 +
# merge.questions takes a multiple checkbox question and merges that into a single factor.
 +
# First levels in levs have priority over others, if several levels apply to a row.
 +
 +
merge.questions <- function(
 +
  dat, # data.frame with questionnaire data
 +
  cols, # list of vectors of column names or numbers to be merged into one level in the factor
 +
  levs, # vector (with the same length as cols) of levels of factors into which questions are merged.
 +
  name # text string for the name of the new factor column in the data.
 +
) {
 +
  for(i in length(cols):1) {
 +
    temp <- FALSE
 +
    for(j in rev(cols[[i]])) {
 +
      temp <- temp | !is.na(dat[[j]])
 +
    }
 +
    dat[[name]][temp] <- levs[i]
 +
  }
 +
  dat[[name]] <- factor(dat[[name]], levels = levs, ordered = TRUE)
 +
  return(dat)
 +
}
  
 
############ Shuffles columns of a data.frame so that they match a pre-defined correlation matrix
 
############ Shuffles columns of a data.frame so that they match a pre-defined correlation matrix

Revision as of 06:44, 13 April 2017



Question

Which functions are so useful that they should be taken into OpasnetUtils package? This page contains draft function which will be included when they are good enough and found important.

Answer

Call the objects stored by this code from another rode with this command:

objects.latest("Op_en6007", code_name = "answer")

+ Show code

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>