Home > front end >  how to create a big data frame from a function with few continuous vectors
how to create a big data frame from a function with few continuous vectors

Time:12-11

I have a function based on 4 main factors. I want the function to produce a dataframe with the results.

gnd <- function(a,b,c,d) {
  e <- a*b
  number_red <- b*e
  number_leaf_red <- b*e*6
  number_blue <- c*d*e
  total_number1<- sum(number_red, number_blue)
  type <- as.factor(c("number_red","number_leaf_red","number_blue"))
  a.prob<-rep(a, 3)
  b.prob<-rep(b, 3)
  value <- c(number_red, number_leaf_red, number_blue)
  data.frame(type,a.prob,b.prob , value)
}

But when I want to do a "for" function on this function, I get a list of dataframes.

a <- c(0.1, 0.2,0.3,0.4)

for (i in a) {
  h<-  gnd(a,0.1,0.3,0.4)
  print(h)}

how can i apply different vectors for each parameter (a,b,c,d)(like the bellow example) in a way that the function will produce one big data frame

a <- c(0.1, 0.2,0.3,0.4)
b <- c(0.1, 0.2,0.3,0.4)
c <- c(0.1, 0.2,0.3,0.4)
d <- c(0.1, 0.2,0.3,0.4)

for (i in x) {
  h<-  gnd(x,2,3,4)
  print(h)}

thanks in advance dor your help

CodePudding user response:

We could use Map and rbind the list output into a single data.frame

do.call(rbind, Map(gnd, a, b, c, d))

-output

             type a.prob b.prob  value
1       number_red    0.1    0.1 0.0010
2  number_leaf_red    0.1    0.1 0.0060
3      number_blue    0.1    0.1 0.0001
4       number_red    0.2    0.2 0.0080
5  number_leaf_red    0.2    0.2 0.0480
6      number_blue    0.2    0.2 0.0016
7       number_red    0.3    0.3 0.0270
8  number_leaf_red    0.3    0.3 0.1620
9      number_blue    0.3    0.3 0.0081
10      number_red    0.4    0.4 0.0640
11 number_leaf_red    0.4    0.4 0.3840
12     number_blue    0.4    0.4 0.0256

Or with for loop

out <- data.frame()
for(i in seq_along(a)) out <- rbind(out, gnd(a[i], b[i], c[i], d[i]))

-output

> out
              type a.prob b.prob  value
1       number_red    0.1    0.1 0.0010
2  number_leaf_red    0.1    0.1 0.0060
3      number_blue    0.1    0.1 0.0001
4       number_red    0.2    0.2 0.0080
5  number_leaf_red    0.2    0.2 0.0480
6      number_blue    0.2    0.2 0.0016
7       number_red    0.3    0.3 0.0270
8  number_leaf_red    0.3    0.3 0.1620
9      number_blue    0.3    0.3 0.0081
10      number_red    0.4    0.4 0.0640
11 number_leaf_red    0.4    0.4 0.3840
12     number_blue    0.4    0.4 0.0256

CodePudding user response:

For different combinations of a,b,c,d you need to use pmap function from tidyverse. Since you have 4 elements each in input total length of df is 768. I edited your code to see the different combinations and results

gnd <- function(a,b,c,d) {
  e <- a*b
  number_red <- b*e
  number_leaf_red <- b*e*6
  number_blue <- c*d*e
  total_number1<- sum(number_red, number_blue)
  type <- as.factor(c("number_red","number_leaf_red","number_blue"))
  a.prob<-rep(a, 3)
  b.prob<-rep(b, 3)
  value <- c(number_red, number_leaf_red, number_blue)
  data.frame(a,b,c,d,type,a.prob,b.prob , value)
}

a <- c(0.1, 0.2,0.3,0.4)
b <- c(0.1, 0.2,0.3,0.4)
c <- c(0.1, 0.2,0.3,0.4)
d <- c(0.1, 0.2,0.3,0.4)
x<-as.list(expand.grid(list(a,b,c,d))) ## creating different combination


df<-pmap_df(list(x$Var1,x$Var2,x$Var3,x$Var4),gnd)
  •  Tags:  
  • r
  • Related