Frage

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)
War es hilfreich?

Lösung

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  

Andere Tipps

You're sort of going about this all wrong.

  1. Creating an empty data frame and then filling it is a very bad design pattern in R. For very small numbers of rows, it's probably not a huge deal, but this will not scale. Every time you add a row, R is forced to copy the entire data frame. This will become very slow.

  2. In general, modifying objects that are defined outside of a function (e.g. pinecone being modifies from within funkytown) is again not something we generally do in R, unlike in other languages. If you want to modify something, pass it (i.e. pinecone) as an argument, and then return it and capture the return value. (You can do it using <<- or assign, but you'll be sorry down the road.)

  3. deparse(substitute(greek)) clearly doesn't do what you think it does. It results in a character "honeybadger$alpha".

See Roland's answer for a more R-ish way of approaching what might be your end goal.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top