Home > Software engineering >  by() function in r returns ugly shaped list buy I wanted a data.frame in long format, what should I
by() function in r returns ugly shaped list buy I wanted a data.frame in long format, what should I

Time:06-19

I am using by() function in r with a large data set to compute some values for each participant and each condition (I use retimes package and mexgauss function). However, the output is not handy and I cannot analyze it further without arrenging it into long data.frame format. I tried many different functions like below, but non of them give the output that i want:

  • data.frame(Reduce(rbind, data))
  • data.frame(matrix(unlist(data, use.names = TRUE), nrow=length(data), byrow=T))
  • unlist(data,recursive = FALSE, use.names = TRUE)
  • do.call(rbind.data.frame, data)
  • data.frame(matrix(unlist(data), nrow=1536, byrow=TRUE),stringsAsFactors=FALSE)

This is the simple reproducible code:

SNO = rep (c(1,2,3,4,5), 6)
color = rep(c("red", "green", "blue"), 10)
condition = rep(c("a", "b", "c", "d", "e"), 6)
values = rep(c(10,20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150), 2)

D = data.frame(SNO, color, condition, values)
meanD = by((D$values), data.frame(D$SNO, D$color, D$condition), mean)

I want my output look like as presented in the picture. What should I use?

CodePudding user response:

Instead of by (returns a list), aggregate would return the output as a data.frame

aggregate(values ~ SNO   color   condition, D, mean)
  • Related