0

I have a function in R that actually takes input a string and manipulates it's contents.

Let's assume this function:

myfunc <- function (string, ...., .... )
  {
      paste("string")
  }

What I want to do is, I want to read the strings from a text file and store it in a data frame. Then, I want to call a function ( which I will have a loop I know ), to which I will pass my data frame, and it should print the contents of the whole file using the function that I just defined.

It will be something like ( assuming n to be the number of strings in the file )

 myfunc2 <- function (dataframe,... , ... )
   {
      for ( i in n:1 )
    {
      myfunc(dataframe[i]
    }
   }

But, this doesn't work. Please help!

  • So, you want to read in the contents of a text file and print it to the screen?? `readLines(x)` – jbaums Jun 15 '14 at 14:11
  • That is not my actual task. I just made a sample function. What I am trying to ask is, how can I pass an entire data frame to a function and then that function should be able to access the data in that data frame. What will be the syntax for that and how should I then call the function so that it is executed on every row of my file. – user3730341 Jun 15 '14 at 14:20
  • 1
    @user3730341 you are looking or `apply`( from the comment) but the question is really really really unclear. From the comment you want to apply a function to each row, the question is to apply a function to each string in the file... – agstudy Jun 15 '14 at 14:21
  • Yup, sounds like it: `apply(df, 1, fun)`. – jbaums Jun 15 '14 at 14:23
  • Let's say I make a function F1. It will modify the contents of a string. That's all. The function F2 actually, takes the data frame as input and to each string of the data frame, the function F1 has to work upon. So, what I actually want to do is, inside the function F2, there will be a loop which will parse upon the strings in the data frame and the function F1, which will do its task. I hope I made it clear now. – user3730341 Jun 15 '14 at 14:26
  • If by string you mean row, then see our previous comments. Your `F2` is `apply`, and your `F1` is `fun` in: `apply(df, 1, F1)`, for data.frame `df`. Take a close look at `?apply`. – jbaums Jun 15 '14 at 14:28
  • And what is 1 here for? – user3730341 Jun 15 '14 at 14:29
  • 1
    ^ take a close look at `?apply` - it's all there :) – jbaums Jun 15 '14 at 14:29
  • Got it. Thanks. Let me try this. :) – user3730341 Jun 15 '14 at 14:32

0 Answers0