0

I am trying to use lapply so that I can apply a custom function on all elements of a vector in R. I am trying to avoid using for loop here .

This is what I have so far:

writeToHDFS <- function(fileName){
  print(fileName)
  hdfs.init()
 modelfile <- hdfs.file(fileName, "w")
  hdfs.write(gaDataUser, modelfile)
 hdfs.close(modelfile)
}

fileNames <- c("gaDataUser", "gaDataSession", "gaDataSources","gaDataEventTracking", "gaDataEcommerce", "gaDataEcommerce2", "gaDataEcommerce3", "gaDataEcommerce4")


lapply(fileNames,writeToHDFS(x))

I have variables with the names mentioned in the character vector fileNames.

What I need to know:

  1. How to pass each string from the vector fileNames to function writeToHDFS since I would like this function to be executed for every element in the vector.

  2. How to use string name to access variables of that name in the function. For example:

At this line,I have to access variable with name same as string passed to fileName variable in the function.

hdfs.write(variableWithData, modelfile)

3. Can I pass the variable fileName to

modelfile <- hdfs.file(fileName, "w")

instead of passing a string for file name ?

CJB
  • 1,759
  • 17
  • 26
systemdebt
  • 4,589
  • 10
  • 55
  • 116
  • 1
    I am pretty amazed with how R community on stack overflow down votes pretty much everything. – systemdebt Jun 02 '16 at 11:11
  • Please read the documentation `?lapply`, look at the examples `example(lapply)` (both are outside resources for SO). Read the great http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-vs-by-vs-aggrega to get an idea how lapply/sapply works. For geting an object from a charater look at `get()` – jogo Jun 02 '16 at 11:16
  • 1
    `a <- "BOD"; x <- get(a); x` – jogo Jun 02 '16 at 11:22
  • Thank @jogo. That helped – systemdebt Jun 02 '16 at 11:47
  • 1
    The problem to me seems to be: `lapply(fileNames, writeToHDFS(x))`. The second argument should be the function only: `lapply(fileNames, writeToHDFS)`. R can pass functions as objects to other functions such as `lapply`. – CJB Jun 02 '16 at 12:54
  • @Bazz: That indeed was the problem but how does it pass the current iterable object to the function when not defined inline? – systemdebt Jun 02 '16 at 12:57

2 Answers2

2

I am trying to use lapply so that I can apply a custom function on all elements of a vector in R

In this situation, you should use tapply:

tapply(fileNames, 1:length(fileNames), writeToHDFS)

lapply, is short for "list-apply", but fileNames is a vector not a list.

Since you are already pretty well-aimed at using lapply, you can learn from ?lapply.

miken32
  • 42,008
  • 16
  • 111
  • 154
Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
1

writeToHDFS(x) gives you return value of function. But you want to pass function so:

lapply(fileNames,writeToHDFS)
jjaskulowski
  • 2,524
  • 3
  • 26
  • 36