Q:

Put Correlation Output of Function into Data Frame

I am performing a lot of correlations of a populations over time. I have split them up accordingly and have put them through a function with lapply. I want to put the output of each correlation into a data frame (i.e.: each row will be the info for one correlation, with the columns: correlation's name, p-value, t statistic, df, CIs, and corcoeff).
I have two issues: 

I don't know how to extract the name of the correlation made in the split
I can get my function to run the correlation on the split (600+ Correlations), but I can't get it to print it into the data frame. To clarify: When I run the function without the loop, it does all 600 Correlations for each group. However, when I add the loop, it produces NULL for all the groups in the split.

Here is what I have thus far:
> head(Birds) #Shortened for this Post
Location      Species   Year Longitude Latitude Section Total Percent  Family
1 Chiswell A  Kittiwake 1976 -149.5847 59.59559 Central   310 16.78397 Gull

BigSplit<-split(Birds,list(Birds$Family, Birds$Location, 
Birds$Section,Birds$Species), drop=T) #A list of Dataframes

#Make empty data frame
resultcor <- data.frame(Name = character(),
                        tvalue = character(),
                        degreeF = character(),
                        pvalue = character(),
                        CIs = character(),
                        corcoeff = character(),stringsAsFactors = F)

WorkFunc <- function(dataset) {
     data.name = substitute(dataset) #Use "dataset" as substitute for actual dataset name

     #Correlation between Year and population Percent
     try({
          correlation <- cor.test(dataset$Year, dataset$Percent, method = "pearson")    
     }, silent = TRUE)

     for (i in 1:nrow(resultcor)) {
          resultcor$Name[i] <- ??? #These ??? are not in the code, just highlighting Issue 1
          resultcor$tvalue[i] <- correlation$dataset$statistic
          resultcor$degreeF[i] <- correlation$dataset$parameter
          resultcor$pvalue[i] <- correlation$dataset$p.value
          resultcor$CIs[i] <- correlation$dataset$conf.int
          resultcor$corcoeff[i] <- correlation$dataset$estimate
     }
}

lapply(BigSplit, WorkFunc)

Any help would be appreciated, Thanks!

A:

Consider using Map (variant to mapply) where you pass both the elements and names of BigSplit. Using Map will output a list of dataframes that you can then row bind at end with do.call(). Below assumes BigSplit is a named list.
WorkFunc <- function(dataset, dataname) {
    # Correlation between Year and population Percent
    tryCatch({ 
        correlation <- cor.test(dataset$Year, dataset$Percent, method = "pearson")
        CIs <- correlation$conf.int

        return(data.frame(
                  Name = dataname,
                  tvalue = correlation$statistic,
                  degreeF = correlation$parameter,
                  pvalue = correlation$p.value,
                  CI_lower = ifelse(is.null(CIs), NA, CIs[[1]]),
                  CI_higher = ifelse(is.null(CIs), NA, CIs[[2]]),
                  corcoeff = correlation$estimate
             )
         ) 
     }, error = function(e) 
             return(data.frame(
                        Name = character(0),
                        tvalue = numeric(0),
                        degreeF = numeric(0),
                        pvalue = numeric(0),
                        CI_lower = numeric(0),
                        CI_higher = numeric(0),
                        corcoeff = numeric(0)
                    )
              )
      )
}    

# BUILD CORRELATION DATAFRAMES INTO LIST
cor_df_list <- Map(WorkFunc, BigSplit, names(BigSplit))
cor_df_list <- mapply(WorkFunc, BigSplit, names(BigSplit), SIMPLIFY=FALSE)   # EQUIVALENT

# ROW BIND ALL DATAFRAMES TO FINAL LARGE DATAFRAME
finaldf <- do.call(rbind, cor_df_list)

