Learn R Programming

userfriendlyscience (version 0.1-2)

basicSPSStranslationFunctions: Basic SPSS translation functions

Description

Basic functons to make working with R easier for SPSS users: getData provides an easy way to load SPSS datafiles; filterBy and useAll allow easy temporary filtering of rows from the dataframe; mediaan and modus compute the median and mode of ordinal or numeric data.

Usage

getData(filename=NULL, errorMessage = "[defaultErrorMessage]",
        use.value.labels=TRUE, to.data.frame=TRUE, stringsAsFactors=TRUE, ...)
filterBy(dat, expression, replaceOriginalDataframe = TRUE, envir = parent.frame())
useAll(dat, replaceFilteredDataframe = TRUE)
mediaan(vector)
modus(vector)

Arguments

filename
It is possible to specify a path and filename to load here. If not specified, the default R file selection dialogue is shown
errorMessage
The error message that is shown if the file does not exist or does not have the right extension; "[defaultErrorMessage]" is replaced with a default error message (and can be included in longer messages).
use.value.labels
Only useful when reading from SPSS files: whether to read variables with value labels as factors (TRUE) or numeric vectors (FALSE).
to.data.frame
Only useful when reading from SPSS files: whether to return a dataframe or not.
stringsAsFactors
Whether to read strings as strings (FALSE) or factors (TRUE).
...
Additional options, passed on to the function used to import the data (which depends on the extension of the file).
dat
Dataframe to process: for filterBy, dataframe to filter rows from; for useAll, dataframe to restore ('unfilter').
expression
Logical expression determining which rows to keep and which to drop. Can be either a logical vector or a string which is then evaluated. If it's a string, it's evaluated using 'with' to evaluate the expression using the variable names.
replaceOriginalDataframe
Whether to also replace the original dataframe in the parent environment. Very messy, but for maximum compatibility with the 'SPSS way of doing things', by default, this is true. After all, people who care about the messiness/inappropriateness of this fun
envir
The environment where to create the 'backup' of the unfiltered dataframe, for when useAll is called and the filter is deactivated again.
replaceFilteredDataframe
Whether to replace the filtered dataframe passed in the 'dat' argument (see replaceOriginalDataframe).
vector
For mediaan and modus, the vector for which to find the median or mode.

Value

  • getData returns the imported dataframe, with the filename from which it was read stored in the 'filename' attribute. mediaan returns the median, or, in the case of a factor where the median is in between two categories, both categories. modus returns the mode.

Examples

Run this code
### Open a dialogue to read an SPSS file
### (wrapped in 'if (interactive())' to prevent execution during
###  the testing of the examples when building the package)
if (interactive()) {
  getData();
}

### Get a median and a mode
mediaan(c(1,2,2,3,4,4,5,6,6,6,7));
modus(c(1,2,2,3,4,4,5,6,6,6,7));

### Create an example dataframe
(exampleDat <- data.frame(x=rep(8, 8), y=rep(c(0,1), each=4)));
### Filter it, replacing the original dataframe
(filterBy(exampleDat, "y=0"));
### Restore the old dataframe
(useAll(exampleDat));

Run the code above in your browser using DataLab