7

Assume I have a function that reads data from a MySQL table, manipulates it and returns some data.frame. Note the function is just an example whose functionality does not matter itself..., E.g.:

addRowSd <- function(table,con,pattern="^Variable") {

dframe <- dbReadTable(con,table)
cn <- colnames(dframe)
qs <- subset(x, x  %in% grep(pattern, x, value=TRUE))
dframe$qsd <- sd(t(dframe[,c(qs)])) 

return(dframe)
}

mydf$sd <- addRowSd(...)

I end up with a data.frame called mydf. Now I´d like to do to this to a character vector of SQL table names AND name the returned dataframes correspondingly. If I just use

x=lapply(MySQLtablenames,addRowSd,con)

I´ll get some list called x. Of course I could unlist and rename everything the way I´d like to, but my question is:

How can I make lapply (or another comparable function) return multple single dataframes or at least a list that contains some names derived from my character vector "MySQLtablenames"?

2
  • possibly it might help to make the addRowSd function assign some name to returned data.frame... Commented Oct 28, 2010 at 10:19
  • 1
    How about names(x) <- MySQLtablenames or in one line x<-setNames(lapply(MySQLtablenames,addRowSd,con),MySQLtablenames) or x<-lapply(setNames(MySQLtablenames,MySQLtablenames),addRowSd,con) Commented Oct 28, 2010 at 10:42

2 Answers 2

11

just found an answer on my own:

assign("somename",dframe,envir = .GlobalEnv)
Sign up to request clarification or add additional context in comments.

4 Comments

You could run into scoping issues with this. E.g if you want to use function inside another function, then your data.frame is created in global environment. You could play with parent.frame to improve your method.
In both your examples, you are inserting a DF into a list (or lapply returns a list). In those cases, the name of the object that is the DF is irrelevant, is it not. What matter is the names for the list components. If I were doing this, I'd store the DFs in a list, set the names on the list after the table names, as per Marek's comment above, and then work with the list. If you want to refer to the individual DFs by name, use with(x, DF.name) where x is list returned by lapply and DF.name is the name of the DF you want to access or some function applied to it.
@Marek, currently the only thing that runs into scoping issues is my brain. the parent.frame stuff is all new to me and I do not get it yet, but thx for pointing to it. What will cause the scoping issues? Loading to much data from the database to the same (global) environment?
4+ years later I am just adding... DON'T do this. It's also not gonna pass CRAN checks.
1

If you supply sapply a character vector, it will name the items in the returned list by the supplied character vector (USE.NAMES default to TRUE)... I would also use simplify=FALSE as depending on the data.frames returned you may get unpredictable results

x=sapply(MySQLtablenames,addRowSd,con, simplify=FALSE)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.