# original data
my_list <- list(time = structure(c(0, 1),
dim = 2:1,
dimnames = list(c("Pre",
"Post"), "Post")),
treatment = structure(c(0, 1),
dim = 2:1,
dimnames = list(
c("Control", "Treatment"), "Treatment")))
# main function
temp <- mapply(function(x,y){
df <- data.frame(temp = paste(rownames(x), x)) # since you combine data from the rownames and data
names(df) <- paste("levels of", y) # since you have customized column names
df
},
my_list,
names(my_list),
SIMPLIFY = FALSE, # prevent doing weird conversions
USE.NAMES = FALSE # don't use the my_list names in result list
)
#combinde to one
yourWantedDf <- cbind.data.frame(temp)
# print
yourWantedDf
# levels of time levels of treatment
# 1 Pre 0 Control 0
# 2 Post 1 Treatment 1
names(df) <- paste("levels of", y) # since you have customized column names
translates in time (first iteration) to
rownames(x)= c("Pre", "Post"), x = c(0,1), y = time
so the output is just c("Pre 0", "Post 1") and the above mentioned line changes the data.frame column name to levels of time. Post gets ignored completely.
and in treatment (second iteration) to
rownames(x)= c("Control", "Treatment "), x = c(0,1), y = treatment
so the output is just c("Control 0", "Treatment 1") and the above mentioned line changes the data.frame column name to levels of treatment. Treatment gets ignored completely.
This is why temp looks like
# [[1]]
# levels of time
# 1 Pre 0
# 2 Post 1
#
# [[2]]
# levels of treatment
# 1 Control 0
# 2 Treatment 1
The USE.NAMES = FALSE refers to the names of your list my_list and not the row or column names of the subobjects.