if (FALSE) {
# generate a test dataframe with 100 (imaginary) participants / units of
# observation (ID), and 8 repeated measurements of variable (X_1, X_2, ...)
dtaInp <- cbind(data.frame(ID = as.character(seq(1:100))),
stats::setNames(
as.data.frame(matrix(runif(800, -10, 10), nrow = 100)),
paste0("X_", 1:8)))
cat(str(dtaInp))
# 'data.frame': 100 obs. of 9 variables:
# $ ID : chr "1" "2" "3" "4" ...
# $ X_1: num ...
# $ X_2: num ...
# $ X_3: num ...
# $ X_4: num ...
# $ X_5: num ...
# $ X_6: num ...
# $ X_7: num ...
# $ X_8: num ...
# this data set is stored as (temporary) RDS-file and later processed by wide2long
nmeInp <- tempfile(fileext = ".rds")
nmeOut <- tempfile(fileext = ".omv")
saveRDS(dtaInp, nmeInp)
jmvReadWrite::wide2long_omv(dtaInp = nmeInp, fleOut = nmeOut, varID = "ID",
varTme = "measure", varLst = setdiff(names(dtaInp), "ID"),
varSrt = c("ID", "measure"))
# it is required to give at least the arguments dtaInp (if dtaInp is a data frame,
# fleOut needs to be provided too) and varID
# "reshape" then assigns all variables expect the variable defined by varID to
# varLst (but throws a warning)
# varSrt enforces sorting the data set after the transformation (sorted, the
# measurements within one person come after another; unsorted all measurements
# for one repetition would come after another)
# check whether the file was created and its size
cat(list.files(dirname(nmeOut), basename(nmeOut)))
# -> "file[...].omv" ([...] contains a random combination of numbers / characters
cat(file.info(nmeOut)$size)
# -> 6939 (approximate size; size may differ in every run [in dependence of how
# well the generated random data can be compressed])
cat(str(jmvReadWrite::read_omv(nmeOut, sveAtt = FALSE)))
# the data set is now transformed into long (and each the measurements is now
# indicated by the "measure")
# 'data.frame': 800 obs. of 3 variables:
# $ ID : Factor w/ 100 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 2 2 ...
# ..- attr(*, "missingValues")= list()
# $ measure: Factor w/ 8 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 1 2 ...
# ..- attr(*, "missingValues")= list()
# $ X : num ...
# ..- attr(*, "missingValues")= list()
unlink(nmeInp)
unlink(nmeOut)
}
Run the code above in your browser using DataLab