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,659
  • 16
  • 26
systemdebt
  • 3,755
  • 10
  • 45
  • 103
  • 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
  • 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.

People have down voted you, because it is highly recommended you first check manual or documentation. Since you are already pretty well-aimed at using lapply, you can learn from ?lapply.

Down vote, is another form of feedback, other than comments and answers. It means this question does not really need the effort from SO community. As an R user, you should be aware that R documentation is the first thing you should look for when getting into trouble.

Zheyuan Li
  • 62,170
  • 17
  • 162
  • 226
1

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

lapply(fileNames,writeToHDFS)
jjaskulowski
  • 2,437
  • 2
  • 24
  • 35