Question

How to transfer dataframe `raw_df` to `list` row by row?

How to transfer data frame raw_df to list row by row? Desired output: wished_list

 raw_df <- data.frame(cat=c('a','b','c'),value=c('high','mid','low'))
 wished_list <- list(a='high',b='mid',c='low')
 4  49  4
1 Jan 1970

Solution

 4

One option would be:

as.list(setNames(raw_df$value, raw_df$cat))

$a
[1] "high"

$b
[1] "mid"

$c
[1] "low"
2024-07-24
tmfmnk

Solution

 4

One way to solve your probem:

split(raw_df$value, raw_df$cat)

$a
[1] "high"

$b
[1] "mid"

$c
[1] "low"
2024-07-24
B. Christian Kamgang

Solution

 2

Simplifying to character (vector) might be another option:

with(raw_df, structure(value, names=cat))
#>      a      b      c 
#> "high"  "mid"  "low"

## with class #> [1] "character"

If list format is no strict requirement.

2024-07-24
Friede

Solution

 1

Or you can use tibble::deframe

> as.list(tibble::deframe(raw_df))
$a
[1] "high"

$b
[1] "mid"

$c
[1] "low"
2024-07-24
ThomasIsCoding

Solution

 1

Using Map():

with(raw_df, Map(\(x,y) y, cat, value))
# $a
# [1] "high"
# 
# $b
# [1] "mid"
# 
# $c
# [1] "low"
2024-07-24
s_baldur