Maria Reyes
Maria Reyes

Reputation: 369

r - Using l_ply to add results to an existing data frame

Is l_ply or some other apply-like function capable of inserting results to an existing data frame?

Here's a simple example...

Suppose I have the following data frame:

mydata <- data.frame(input1=1:3, input2=4:6, result1=NA, result2=NA)

  input1 input2 result1 result2
1      1      4      NA      NA
2      2      5      NA      NA
3      3      6      NA      NA

I want to loop through the rows, perform operations, then insert the answer in the columns result1 and result2. I tried:

l_ply(1:nrow(mydata), function(i) {
  mydata[i,"result1"] <- mydata[i,"input1"] + mydata[i,"input2"]
  mydata[i,"result2"] <- mydata[i,"input1"] * mydata[i,"input2"]})

but I get back the original data frame with NA's in the result columns.

P.S. I've already read this post, but it doesn't quite answer my question. I have several result columns, and the operations I want to perform are more complicated than what I have above so I'd prefer not to compute the columns separately then add them to the data frame after as the post suggests.

Upvotes: 0

Views: 102

Answers (2)

IRTFM
IRTFM

Reputation: 263332

I suppose there might be a plyr approach but this seems very easy and clear to do in base R:

> mydata[3:4] <- with(mydata, list( input1+input2, input1*input2) )
> mydata
  input1 input2 result1 result2
1      1      4       5       4
2      2      5       7      10
3      3      6       9      18

Even if you got that plyr code to deliver something useful, you are still not assigning the results to anything so the it would have evaporated under the glaring sun of garbage collection. And do note that if you followed the advice of @Vlo you would have seen a result at the console that might have led you to think that 'mydata' was updated, but the 'mydata'-object would have remained untouched. You need to assign values back to the original object. For dplyr operations you are generally going to be assigning back entire objects.

Upvotes: 1

RHertel
RHertel

Reputation: 23788

You don't need to use apply or variations thereof. Instead, you can exploit that R is vectorized:

mydata$result1 <- mydata$input1 + mydata$input2
mydata$result2 <- mydata$input1 * mydata$input2
#> mydata
#  input1 input2 result1 result2
#1      1      4       5       4
#2      2      5       7      10
#3      3      6       9      18

Upvotes: 0

Related Questions