Convert data frame to list of lists by row I am rewriting some old code where I take a dataframe in r and convert it using the tidyverse packages to a list of lists, where each element is one row of the original dataframe - which is itself a list with each column an element.

My previous function achieved it like so:

library(tidyverse)

feat &lt;- function(coords, height = 20, vjust = 75,  fill = "orange"){
  inst &lt;- purrr::flatten(purrr::by_row(coords, ..f = function(x) 
    rec &lt;- list(type = "rect", 
                x = as.numeric(x[1]), 
                y = vjust, 
                width = as.numeric(x[2]-x[1]),
                height = height, 
                fill = fill), 
    .labels = FALSE))
  return(inst)
}


However by_row() has been depreciated from the purrr package and so I would like to rewrite it. This is my attempt:

feat2 &lt;- function(coords, height = 20, vjust = 75,  fill = "orange"){
  inst &lt;- coords %&gt;% 
    mutate(type = "rect", 
           x = as.numeric(start), 
           y = vjust, 
           width = as.numeric(end-start),
           height = height, 
           fill = fill) %&gt;%
    select(-start, -end) %&gt;% 
    mutate(count = 1:n()) %&gt;%
    nest(-count) %&gt;%
    select(-count) %&gt;% 
    mutate(data = map(data, ~ flatten(.x))) %&gt;% pull()
  return(inst)
}


which does the job but I feel there should be a quicker, more elegant way to achieve this. Do you have any ideas on that aspect?

Here is an example data set:

coords &lt;- structure(list(start = c(126, 433, 603, 1604), end = c(327, 495, 
644, 1831)), .Names = c("start", "end"), row.names = c(NA, -4L
), class = "data.frame")


and the desired output:

result &lt;- list(structure(list(type = "rect", x = 126, y = 75, width = 201, 
    height = 20, fill = "blue"), .Names = c("type", "x", "y", 
"width", "height", "fill")), structure(list(type = "rect", x = 433, 
    y = 75, width = 62, height = 20, fill = "blue"), .Names = c("type", 
"x", "y", "width", "height", "fill")), structure(list(type = "rect", 
    x = 603, y = 75, width = 41, height = 20, fill = "blue"), .Names = c("type", 
"x", "y", "width", "height", "fill")), structure(list(type = "rect", 
    x = 1604, y = 75, width = 227, height = 20, fill = "blue"), .Names = c("type", 
"x", "y", "width", "height", "fill")))
without any external packages:

feat3 &lt;- function(coords, height = 20, vjust = 75,  fill = "blue"){
  xx &lt;- apply(coords, 1, function(x) {
    list(type = "rect",
         x = as.numeric(x[1]), 
         y = vjust, 
         width = as.numeric(x[2] - x[1]),
         height = height,
         fill = fill)
    }
  )
  return(xx)
}

all.equal(feat3(coords), result)
# [1] TRUE


Benchmarks:

microbenchmark::microbenchmark(
  feat3(coords),
  feat2(coords), unit = "relative"
)
# Unit: relative
# expr      min       lq     mean   median       uq      max neval cld
# feat3(coords)   1.0000   1.0000   1.0000   1.0000   1.0000   1.0000   100  a 
# feat2(coords) 315.8261 297.2919 254.5025 258.7822 248.2652 286.9575   100   b