Hello, I have a big problem which I’m just not able to solve. I created the following mean value from the following dataset structure: Id |value 1 | 2 1 | 3 1 | 4 2 | 2 2 | 1 3 | 5 4 | 3 etc.|etc. with the command: mean_rating <- tapply(ratok$value, ratok$project_id , mean,simplify = FALSE) this gives me a ragged array:> mean_rating [1]$`14` ###########==project_id [1] 3.933333 #######==mean value> dim (mean_rating)[1] 2214 I want to separate now the project_id from the mean value. So that I have two separate columns (2 dimension). How can I separate a ragged array into two variables? *Additional information:* I need this information to put the mean value into another dataset (“projok”) at the correct project_id. I would do that as follow: k=projok[,1] for(i in 1:2442) { projLineNb = which(mean_rating$project_id==k[i]) projok$mean[i] = mean_rating$value[projLineNb] } But with a ragged array I can not refer to project_id. Or is there a possibility that I can refer to the project_id in a ragged array?> mean_rating [[1]][1] 3.933333 ==I can refer to the value only Thank you very much Best, Helene [[alternative HTML version deleted]]
if you set parameter simplify=TRUE, it returns a vector of the ragged mean. In your case, mean_rating <- tapply(ratok$value, ratok$project_id , mean,simplify = TRUE) df<-data.frame(ID=dimnames(mean_rating)[[1]], mean=mean_rating) Weidong Gu On Sun, Oct 16, 2011 at 9:53 AM, Helene Schreyer <helene.schreyer at gmail.com> wrote:> Hello, > > > I have a big problem which I?m just not able to solve. > > > > I created the following mean value from the following dataset structure: > > > Id |value > > 1 ?| 2 > > 1 ?| 3 > > 1 ?| 4 > > 2 ?| 2 > > 2 ?| 1 > > 3 ?| 5 > > 4 ?| 3 > > etc.|etc. > > > > with the command: > > mean_rating <- tapply(ratok$value, ratok$project_id , mean,simplify = FALSE) > > > > this gives me ?a ragged array: > >> mean_rating [1] > > $`14` ###########==project_id > > [1] 3.933333 #######==mean value > > > > > >> dim (mean_rating) > > [1] 2214 > > > > I want to separate now the project_id from the mean value. So that I have > two separate ? ? ? ? columns (2 dimension). > > > > How can I separate a ragged array into two variables? > > > > > > > > *Additional information:* > > I need this information to put the mean value into another dataset > (?projok?) at the correct project_id. > > > > I would do that as follow: > > k=projok[,1] > > > > for(i in 1:2442) { > > ? ? ? ? ? ? ? ?projLineNb = which(mean_rating$project_id==k[i]) > > ? ? ? ? ? ? ? ?projok$mean[i] = mean_rating$value[projLineNb] > > } > > > > But with a ragged array I can not refer to project_id. Or is there a > possibility that I can refer to the project_id in a ragged array? > > > >> mean_rating [[1]] > > [1] 3.933333 ==I can refer to the value only > > > > Thank you very much > > Best, > > Helene > > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >
Hi: Try this: ratok <- data.frame(Id = rep(1:3, 3:1), value = c(2, 3, 4, 2, 1, 5)) aggregate(value ~ Id, data = ratok, FUN = mean) Id value 1 1 3.0 2 2 1.5 3 3 5.0 aggregate() returns a data frame with the Id variable and mean(value). HTH, Dennis On Sun, Oct 16, 2011 at 6:53 AM, Helene Schreyer <helene.schreyer at gmail.com> wrote:> Hello, > > > I have a big problem which I?m just not able to solve. > > > > I created the following mean value from the following dataset structure: > > > Id |value > > 1 ?| 2 > > 1 ?| 3 > > 1 ?| 4 > > 2 ?| 2 > > 2 ?| 1 > > 3 ?| 5 > > 4 ?| 3 > > etc.|etc. > > > > with the command: > > mean_rating <- tapply(ratok$value, ratok$project_id , mean,simplify = FALSE) > > > > this gives me ?a ragged array: > >> mean_rating [1] > > $`14` ###########==project_id > > [1] 3.933333 #######==mean value > > > > > >> dim (mean_rating) > > [1] 2214 > > > > I want to separate now the project_id from the mean value. So that I have > two separate ? ? ? ? columns (2 dimension). > > > > How can I separate a ragged array into two variables? > > > > > > > > *Additional information:* > > I need this information to put the mean value into another dataset > (?projok?) at the correct project_id. > > > > I would do that as follow: > > k=projok[,1] > > > > for(i in 1:2442) { > > ? ? ? ? ? ? ? ?projLineNb = which(mean_rating$project_id==k[i]) > > ? ? ? ? ? ? ? ?projok$mean[i] = mean_rating$value[projLineNb] > > } > > > > But with a ragged array I can not refer to project_id. Or is there a > possibility that I can refer to the project_id in a ragged array? > > > >> mean_rating [[1]] > > [1] 3.933333 ==I can refer to the value only > > > > Thank you very much > > Best, > > Helene > > ? ? ? ?[[alternative HTML version deleted]] > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >