# generate a test dataframe with 100 (imaginary) participants / units of
# observation (ID), 10 measurement (measure) of two variables (V1, V2)
dtaInp <- data.frame(ID = rep(as.character(seq(1, 100)), each = 10),
Measure = rep(seq(1, 10), times = 100),
V1 = runif(1000, 0, 100),
V2 = round(rnorm(1000, 100, 15)))
cat(str(dtaInp))
# the output should look like this
# 'data.frame': 1000 obs. of 4 variables:
# $ ID : chr "1" "1" "1" "1" ...
# $ Measure: int 1 2 3 4 5 6 7 8 9 10 ...
# $ V1 : num ...
# $ V2 : num ...
# this data set is stored as (temporary) RDS-file and later processed by long2wide
nmeInp <- tempfile(fileext = ".rds")
nmeOut <- tempfile(fileext = ".omv")
saveRDS(dtaInp, nmeInp)
jmvReadWrite::aggregate_omv(dtaInp = nmeInp, fleOut = nmeOut, varAgg = c("V1", "V2"),
grpAgg = "ID", clcN = TRUE, clcMn = TRUE, clcSD = TRUE)
# it is required to give at least the arguments dtaInp, varAgg and grpAgg, each of
# the different switches (clc...) requests a aggregation measure (e.g., mean, median,
# SD, IQR, etc.) to be calculated
# 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)
# -> 4898 (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 contains now the ID variable identifying the different steps of
# aggregation and one column for each combination of aggregation variable (V1 / V2)
# and which calculation was requested (N, mean and SD)
# 'data.frame': 100 obs. of 7 variables:
# $ ID : chr "1" "10" "100" "11" ...
# ..- attr(*, "jmv-id")= logi TRUE
# ..- attr(*, "missingValues")= list()
# $ V1_N : int 10 10 10 10 10 10 10 10 10 10 ...
# ..- attr(*, "jmv-desc")= chr "V1 (N)"
# ..- attr(*, "missingValues")= list()
# $ V1_Mn: num 45.4 51.9 49.4 54.8 47.2 ...
# ..- attr(*, "jmv-desc")= chr "V1 (Mean)"
# ..- attr(*, "missingValues")= list()
# $ V1_SD: num 31.7 31.4 26.5 20.2 29.1 ...
# ..- attr(*, "jmv-desc")= chr "V1 (SD)"
# ..- attr(*, "missingValues")= list()
# $ V2_N : int 10 10 10 10 10 10 10 10 10 10 ...
# ..- attr(*, "jmv-desc")= chr "V2 (N)"
# ..- attr(*, "missingValues")= list()
# $ V2_Mn: num 96.4 102.3 101.6 104.6 108.7 ...
# ..- attr(*, "jmv-desc")= chr "V2 (Mean)"
# ..- attr(*, "missingValues")= list()
# $ V2_SD: num 14.8 18.4 11.2 10.1 14.3 ...
# ..- attr(*, "jmv-desc")= chr "V2 (SD)"
# ..- attr(*, "missingValues")= list()
unlink(nmeInp)
unlink(nmeOut)
Run the code above in your browser using DataLab