Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

R data.table apply function to rows using columns as arguments which returns a vector (i.e. multiple values)

$
0
0

Let say I have below data.table-

library(data.table)
x = structure(list(f1 = 1:3, f2 = 3:5), .Names = c("f1", "f2"), row.names = c(NA, -3L), class = c("data.table", "data.frame"))

Now I want to apply below function on every rows-

func.text <- function(arg1,arg2){ return(c(10, arg1 + exp(arg2)))}

With that, I see below result-

> x[, func.text(f1, f2), by = seq_len(nrow(x))]
   seq_len        V1
1:       1  10.00000
2:       1  21.08554
3:       2  10.00000
4:       2  56.59815
5:       3  10.00000
6:       3 151.41316

However, I wanted to get 2 extra columns in the final result because this function returning 2 values, same number of rows as with original data.table.

Is there any way to do this?


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>