tlmoore
tlmoore

Reputation: 401

Add column to each data frame in a list based on a value in a separate list of objects

I have a directory containing 28 FCS files (raw data from a flow cytometer). I am converting the list of 'flowFrame' objects into a list of data frames, and I would like to create a column in each data frame that refers to the file name.

library(flowCore)

list = list.files(pattern = "*.fcs") # create a list with all of the files
list = lapply(list, read.FCS) # import all of the FCS files

# Convert the list of FCS files into a list of data frames
df = lapply(list, exprs)
df = lapply(df, as.data.frame)

I can get the file name easily from list by:

list[[1]]@description[['$FIL']]

Moreover, I can manually create the column, but I know there must be a better/faster way!

df[[1]]$sample = list[[1]]@description[['$FIL']]

Upvotes: 1

Views: 161

Answers (1)

akrun
akrun

Reputation: 887128

If we need to get all the description, elements loop over the list with lapply/sapply/Map and use anonymous function call

dfNew <- Map(function(x, y)  transform(y, 
         sample = x@description[["$FIL"]]), list, df)

Upvotes: 1

Related Questions