Search code examples
rfunctionscoping

Scoping issue with using function to add rows to data frame?


I wonder if this might be a scoping problem. I am new to R and am trying to use a function on a series of columns and outputing the results to a new data frame. The function should add the results in a new row.

Below is a simplified version of the code. Currently, the function does not add any data to the ouput data frame ("pinecone"). Can anyone tell me why this isn't working? Many thanks in advance!

## create random data frame
honeybadger <- data.frame(alpha = sample(20:35,100, replace=T),
                      beta = sample(1:35,100, replace=T),
                      gamma = sample(200:301,100, replace=T),
                      delta = sample(40:100,100, replace=T), 
                      epsilon = sample(10:45,100, replace=T))

## create empty data frame for results
pinecone <- data.frame("col_A" = numeric(),
                   "col_B" = numeric(),
                   "col_C" = numeric(),
                   "col_D" = numeric())

## define function to analyze data and add to results data frame
funkytown <- function(greek,rand_int) {
  parameter <- deparse(substitute(greek))
  pinecone[parameter,] <- list("col_A" = mean(greek),
                     "col_B" = rand_int+3,
                     "col_C" = sd(greek),
                     "col_D" = rand_int)
}

## run function on data columns
funkytown(honeybadger$alpha,2)
funkytown(honeybadger$beta,5)
funkytown(honeybadger$gamma,4)
funkytown(honeybadger$delta,1)
funkytown(honeybadger$epsilon,9)

Solution

  • I'm not quite sure what you goal is here. Maybe this:

    set.seed(42)
    honeybadger <- data.frame(alpha = sample(20:35,100, replace=T),
                              beta = sample(1:35,100, replace=T),
                              gamma = sample(200:301,100, replace=T),
                              delta = sample(40:100,100, replace=T), 
                              epsilon = sample(10:45,100, replace=T))
    
    numbers <- sample.int(20, 5)
    
    t(mapply(function(x,y) data.frame(
      col_A=mean(x),
      col_B = y+3,
      col_C = sd(x),
      col_D = y
      ), honeybadger, numbers))
    
    #        col_A col_B col_C    col_D
    #alpha   27.92 6     4.751778 3    
    #beta    18.69 7     9.91244  4    
    #gamma   244.5 13    29.22138 10   
    #delta   70.19 17    18.09609 14   
    #epsilon 26.08 5     10.5569  2