2

I've run into a problem in defining a function in R. I'm trying to convert the contents of a for loop into a function, so that I can lapply the function to a list, (and in turn mclapply using the multicore package)

I need to build a data frame row by row, taking one text and one numerical value at a time from the function. I understand from a previous post that I must return the values somehow, because of R's pass-and-copy rather than pass-by-reference scheme (see: How to add a column in the data frame within a function

Here's a simplified version of what I'm doing:

letters <- c('aa', 'bb', 'cc', 'aa', 'bb', 'dd', 'cc', 'dd', 'ee', 'ee')
numbers <- 1:10
madeup <- data.frame(letters, numbers)
output <- data.frame()

functest <- function(x) {
    subtest <- subset(madeup, letters==x)
    if (nrow(subtest==2)){
        calc <- subtest$numbers[2] - subtest$numbers[1]
        ...WHAT GOES HERE?...
    }
}

Anyone know what I should do to return x and calc so that I can then rbind these two values each into a new row of the output data frame?

If I can copy these two values from the function, I'm imagining the execution command will look something like ---

output <- rbind(output, [mc]lapply(letters, functest))

--- but am happy to be corrected on this!

Community
  • 1
  • 1
cainesap
  • 110
  • 1
  • 1
  • 8
  • if (nrow(subtest==2)) is probably not what you meant, but it's not really clear anyway - try rephrasing your question so that it is clear what you want the function to do, it looks like you want something to happen if by passing in x, "subtest" is two rows from "madeup", but . . . how about posting the for loop you want to convert so we at least have that? – mdsumner Jul 05 '11 at 09:06
  • @mdsumner Hi - 'if (nrow(subtest==2))' is what i mean – cainesap Jul 05 '11 at 09:17
  • @mdsumner (oops, didn't realise return adds the comment; apologies .. newbie ..) So, yeah, that if statement is what i mean, and i decided to show an analogy of the for loop, because what i'm working with isn't easily reproducible. All I need is the x values which have 2 rows and the result of the subtraction calculation shown .. so that i can put these in a data frame – cainesap Jul 05 '11 at 09:20
  • 1
    Update the question to be accurate please – mdsumner Jul 05 '11 at 10:07

1 Answers1

2

rbinding data.frames is very slow in my experience, but anyway: replace your placeholder with return(data.frame(x,calc)).

After changing the function, probably the easiest way to get them all into one data.fameis through:

output<-do.call(rbind, lapply(letters, functest))
Nick Sabbe
  • 11,684
  • 1
  • 43
  • 57
  • @James - yes you're right, I do do that in my original code, but forgot that step here: i should have added uniqletters <- unique(letters), and then it would be output<-do.call(rbind, lapply(uniqletters, functest)) – cainesap Jul 05 '11 at 10:13
  • Is the 'placeholder' == '*subset*' in the Question Code? If you mean 'x' instead, do mean every one or just the one in `function()` – leerssej Jun 15 '16 at 22:05