r - Using dplyr to transform two columns to 625 columns -


i have function my_function takes 2 arbitrary-length numeric vectors , transforms single numeric vector has 625 dimensions.

i have dataframe data includes 3 columns: factor group, numeric variable x, , numeric variable y.

i want operation along these lines:

library(dplyr)  data %>%   group_by(group) %>%   summarize(my_625_new_columns=my_function(x, y)) 

the result should 626 column dataframe: 1 column group , 625 columns corresponding each element in output of my_function. there should 1 row per group.

is possible within dplyr's framework?

you use ddply plyr this. do works dplyr.

## data set.seed(0) dat <- data.frame(group=factor(rep(1:2, 100)), x=rnorm(100), y=rnorm(100))  ## random function apply groups ## returns data.frame same number of columns length of x , y func <- function(x, y) as.data.frame(t( (x+y)/2 ))  library(plyr) res <- ddply(dat, .(group), function(x) {     func(x$x, x$y) }) res #   group        v1        v2         v3         v4         v5         v6 # 1     1  1.022407 0.3569047 -0.3578722 -1.1046582 -0.2532319  0.1755368 # 2     2 -0.551505 0.6595048 -0.4816156  0.6653634  2.0414753 -0.8856480 # ...  ## `do` dplyr library(dplyr) dat %>% group_by(group) %>% do(func(.$x, .$y)) 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -