0

I am trying to convert a two-column dataframe to a named list. There are several solutions on StackOverflow where every value in the first column becomes the 'name', but I am looking to collapse the values in column 2 into common values in column 1.

For example, the list should look like the following:

# Create a Named list of keywords associated with each file. 
fileKeywords <- list(fooBar.R = c("A","B","C"),
                 driver.R = c("A","F","G"))

Where I can retrieve all keywords for "fooBar.R" using:

# Get the keywords for a named file
fileKeywords[["fooBar.R"]]

My data frame looks like:

df <- read.table(header = TRUE, text = "
file        keyWord
'fooBar.R'  'A'
'fooBar.R'  'B'
'fooBar.R'  'C'
'driver.R'  'A'
'driver.R'  'F'
'driver.R'  'G'
")

I'm sure there is a simple solution that I am missing.

Tim
  • 929
  • 12
  • 30

2 Answers2

1

You could use unstack:

as.list(unstack(rev(df)))

$driver.R
[1] "A" "F" "G"

$fooBar.R
[1] "A" "B" "C"

This is equivalent to as.list(unstack(df, keyWord~file))

Onyambu
  • 67,392
  • 3
  • 24
  • 53
0

We can use stack in base R

stack(fileKeywords)[2:1]

if it is the opposite, then we can do

with(df, tapply(keyWord, file, FUN = I))

-output

#$driver.R
#[1] "A" "F" "G"

#$fooBar.R
#[1] "A" "B" "C"
akrun
  • 874,273
  • 37
  • 540
  • 662