How to convert this small list to dataframe?

I have this small list:

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")))

# that gives me: 
$time
     Post
Pre     0
Post    1

$treatment
          Treatment
Control           0
Treatment         1

I want to convert it to dataframe that looks like this:

AOYV0

I tried this:
https://stackoverflow.com/questions/4227223/convert-a-list-to-a-data-frame

but it did not give me my desired df. Any help much appreciated.

Hi @Andrzej ,

hope this helps

# 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

Thank you very much,

If I may, small question, what do x and y are for over there as arguments, please ?
What are they representing ?

Sorry, should have made a comment in the code, in this case x = my_list and y = names(my_list).

The rest (SIMPLIFY, USE.NAMES ) are options for mapply.

So here the general explanation for mapply

mapply(function(x, y, z, w){
                  whatever you want to do
},
object for x,
object for y,
object for z,
object for w,
options for mapply
)

Thank you for your kind reply and explanation.
Much appreciated.

Just one more thing.
This is my_list:
obraz

How did you and mapply get rid of "Post" and "Treatment" , because when I have changed

USE.NAMES = FALSE

to:

USE.NAMES = TRUE

it doesn't look like it changes anything ?

This line of code

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.

Thank you, very helpful and things become clear to me.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.