5

I am trying to use apply and a user defined function on a data.frame. Inside the function I would like to use the column name (for a title of a plot), but apply seems to strips the column name and passes only a vector. MWE:

trialData <- data.frame('a' = rnorm(100),
                        'b' = rnorm(100),
                        'c' = rnorm(100))

someData <- function(dataInput){
  return(colnames(dataInput))
}

dataOutput <- apply(trialData, 2, someData)

print(dataOutput)

returns NULL. Is there any way of accessing the column name inside the function?

Lespied
  • 322
  • 2
  • 9
  • The *apply functions do not give you access to the "attributes" of the data that is being looped through. There are some roundabout ways to access the colnames from within *apply, but my question is what are you trying to achieve with this code? Based on your post there is no need for the call to `apply`. – emilliman5 Feb 15 '17 at 13:09
  • 1
    I'm doing quite a lot of things inside my actual function, a few things require the name of the column. For instance, I generate a few figures and I'd like to use the name in the title. I guess if I cant acess the attributes, I'd need to rewrite the function so that it takes a string, and the call to apply so that it takes two arguments. – Lespied Feb 15 '17 at 13:14
  • First, I consider using lapply to iterate over your columns. Second, instead of passing the dataframe to lapply, pass the column names and then subset from the dataframe for your intended operations. – emilliman5 Feb 15 '17 at 13:18
  • lapply(colnames(trialData), function(x) myfun(trialData[, x] ) will return a list object with plots. Then you can loop through the list and call print to draw figures – Sathish Feb 15 '17 at 13:18

1 Answers1

4

Thanks to the commentors I arrived at the below which give me my desired result.

trialData <- data.frame('a' = rnorm(100),
                        'b' = rnorm(100),
                        'c' = rnorm(100))

someData <- function(dataInput){
  # lots of code here
  return(
    dataName = colnames(dataInput)
  )
}

dataOutput <- lapply(colnames(trialData), function(x){someData(trialData[x])})

print(dataOutput)
Lespied
  • 322
  • 2
  • 9