Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate multiple columns from one function and add them to data.frame

I want to return multiple results from a function on columns of data.frame and add these new columns to the same data.frame together with other simple calculation.

For a simplified example, if I want to get both integral value and absolute error of sin function together with the mid points of integral intervals:

df <- data.frame(Lower = c(1,2,3), Upper = c(2,3,4))
setDT(df)
getIntegral <- function(l, u) {
  n <- integrate(sin, mean(l), mean(u))
  list(Value=n$value, Error=n$abs.error)
}
df[,
   c('Value', 'Error', 'Mid') := {
     n <- getIntegral(Lower, Upper)
     list(n$Value,
          n$Error,
          (Lower+Upper)/2)
   }]
df
   Lower Upper     Value        Error Mid
1:     1     2 0.5738457 6.370967e-15 1.5
2:     2     3 0.5738457 6.370967e-15 2.5
3:     3     4 0.5738457 6.370967e-15 3.5

I don't quite like my approach because separating names of new columns and the values assigned to them makes it hard for me to read, how can I do this task better? It's part of a long data processing chain so I don't want create temp variable outside, so I would prefer solutions using data.table or dplyr alone.

like image 607
user3684014 Avatar asked Dec 29 '14 20:12

user3684014


1 Answers

The RHS should be a list of values, and each element of the list gets converted to a column (and recycled if necessary).

Your function already returns a list (of length 1 each) and (Lower+Upper)/2 returns a vector of 3 values (here). In order to return a list, you can use the function c() as follows:

df[, c('Value', 'Error', 'Mid') := c(getIntegral(Lower, Upper), list((Lower+Upper)/2))]
#    Lower Upper     Value        Error Mid
# 1:     1     2 0.5738457 6.370967e-15 1.5
# 2:     2     3 0.5738457 6.370967e-15 2.5
# 3:     3     4 0.5738457 6.370967e-15 3.5

This makes use of the fact that c(list, list) results in a concatenated list.

like image 65
Arun Avatar answered Sep 30 '22 13:09

Arun