Question

Overall situation:

The interface of my measuring devices couldn’t save any further information but the name of the csv it generates during measuring its values. So I used a systematic set of abbreviations to account for changing parameters, such as concentrations, enzymes, feed stocks, buffers etc., That combined formed the title of my csv files which form the names of the data.frames , where I am now trying to read out the names, to combine them with the rest of the data, to form tables that I can use to do regressions.

The Issue:

I just noticed that I lose the names of my data.frames inside the list, I could rename them after each call of lapply, but this doesn't seam to be a proper solution.

I found suggestion to use the llply, but I can't teach it to keep names either.

# loads plyr package
library(plyr)

# generates a showcase list of dataframes, 
data <- list(data.frame(c(1,2),c(3,3)),data.frame(c(1,2),c(3,3)),data.frame(c(1,2),c(3,3)),data.frame(c(1,2),c(3,3)))

# assigns names to dataframe
names(data) <- list("one","two", "tree", "four")

usses the dataframes name to pass “o” to a column, this part works fine, But after running it the names are lost

data <- lapply(X = seq_along(data), 
  FUN = function(i){    
  x <- data[[i]]
  if (gsub("([(a-z)]).*","\\1", names(data)[i]) == "o") {x$enz <- "o"}
  return(x)}, 
  USE.NAMES = TRUE)  

Same thing with llply, operates as expected but doesn’t keep the name either although I thought I could solve that particular problem (quote: “llply is equivalent to lapply except that it will preserve labels and can display a progress bar.”)

data <- llply(seq_along(data), function(i){    
                 x <- data[[i]]
                 if (gsub("([(a-z)]).*","\\1", names(data)[i]) == "o") {x$enz <- "o"}
                 return(x)})  

I would very much appreciate a hint how to solve this with out something like

name(data) <- list.with.the.names

after each llply ore lapply call.

Was it helpful?

Solution

Do something like this:

for (i in seq_along(data)) data[[i]]$name <- names(data)[i]

do.call(rbind, data)
#       c.1..2. c.3..3. name
#one.1        1       3  one
#one.2        2       3  one
#two.1        1       3  two
#two.2        2       3  two
#tree.1       1       3 tree
#tree.2       2       3 tree
#four.1       1       3 four
#four.2       2       3 four

And continue from there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top