How to save dataframe as nested list in a specified format.

I have a dataframe and want to turn the dataframe into a list of lists that is in this exact format below to be ingested into an api. I cannot figure out how to do this. Mine also include the column name and not just index values. Any assistance would be appreciated.

Here is the format of the nested list:
x <- list( list( "value", "value", "value" ), list( "value", "value", "value") )

Here is the dataframe sample:

emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25))

Hi and welcome! It's not totally clear what your desired output should be, but purrr::transpose() does something like what you're looking for.

emp.data <- data.frame(
  emp_id = c (1:5),
  emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
  salary = c(623.3,515.2,611.0,729.0,843.25)
)

purrr::transpose(emp.data)
#> [[1]]
#> [[1]]$emp_id
#> [1] 1
#> 
#> [[1]]$emp_name
#> [1] "Rick"
#> 
#> [[1]]$salary
#> [1] 623.3
#> 
#> 
#> [[2]]
#> [[2]]$emp_id
#> [1] 2
#> 
#> [[2]]$emp_name
#> [1] "Dan"
#> 
#> [[2]]$salary
#> [1] 515.2
#> 
#> 
#> [[3]]
#> [[3]]$emp_id
#> [1] 3
#> 
#> [[3]]$emp_name
#> [1] "Michelle"
#> 
#> [[3]]$salary
#> [1] 611
#> 
#> 
#> [[4]]
#> [[4]]$emp_id
#> [1] 4
#> 
#> [[4]]$emp_name
#> [1] "Ryan"
#> 
#> [[4]]$salary
#> [1] 729
#> 
#> 
#> [[5]]
#> [[5]]$emp_id
#> [1] 5
#> 
#> [[5]]$emp_name
#> [1] "Gary"
#> 
#> [[5]]$salary
#> [1] 843.25

Created on 2020-05-28 by the reprex package (v0.3.0)

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