Dear R-helpers, my little function below calculates the group score (tmpGroupMean) of an item, appends a "_mean" on its name and stores its value on this name. However, it does not calculate the mean of these scores (LVMean) in the same row correctly, as you can see in the below output which strangely also contains NAs. What is wrong??
Thanks for any hint!!! ###### ## start groupMeanForAllItems <- function(inputDataFrame, groupVector) { # build up result dataframe starting with group means result <- data.frame(unique(groupVector)); # build up result column labels starting with group name resultNames <- "groupname"; for (itemName in names(inputDataFrame)) { # compute mean by group tmpGroupMean <- calculateGroupMean(inputDataFrame[, itemName], groupVector); result <- cbind(result, tmpGroupMean); # generate new column name newColumnName <- sprintf("%s_mean", itemName); resultNames <- c(resultNames, newColumnName) } LVMean = mean(result, na.rm=TRUE); result <- cbind(result, LVMean) resultNames <- c(resultNames, "LVMean") names(result) <- resultNames; return(result) } rPlanning <- data.frame(plan1, plan2, plan3, plan4, plan5, plan6, plan7) a <- groupMeanForAllItems(rPlanning, u_proj) print(a) ## end ######## Output: groupname plan1_mean plan2_mean plan3_mean plan4_mean plan5_mean plan6_mean plan7_mean LVMean 1 a 4.375.000 5.625.000 5.500.000 5.250.000 6.500.000 4.375.000 5.125.000 NA 2 b 6.400.000 6.200.000 6.600.000 6.600.000 6.000.000 4.000.000 5.600.000 5.890.628 3 c 6.400.000 6.400.000 6.600.000 6.800.000 5.400.000 6.200.000 5.600.000 5.789.459 4 d 6.000.000 5.666.667 5.000.000 5.666.667 5.333.333 4.333.333 5.000.000 5.821.688 5 e 4.750.000 3.250.000 4.000.000 4.500.000 4.500.000 2.250.000 2.250.000 5.866.753 6 f 3.000.000 4.250.000 3.500.000 5.000.000 4.500.000 2.000.000 4.750.000 5.943.680 7 g 6.000.000 6.750.000 6.000.000 5.750.000 5.000.000 5.250.000 3.750.000 5.091.667 8 h 6.000.000 5.666.667 5.666.667 6.000.000 5.000.000 3.666.667 5.666.667 5.260.368 9 i 7.000.000 3.000.000 3.500.000 6.500.000 4.500.000 5.000.000 6.500.000 NA 10 j 6.400.000 5.800.000 6.400.000 6.200.000 6.600.000 5.400.000 6.200.000 5.890.628 11 k 6.000.000 6.000.000 6.000.000 4.000.000 5.000.000 6.000.000 6.000.000 5.789.459 12 l 6.500.000 6.750.000 7.000.000 6.750.000 7.000.000 6.250.000 7.000.000 5.821.688 13 m 6.666.667 6.500.000 6.666.667 6.500.000 6.500.000 5.833.333 5.500.000 5.866.753 14 n 6.571.429 6.142.857 6.142.857 5.857.143 5.714.286 5.428.571 4.714.286 5.943.680 15 o 6.666.667 6.666.667 5.666.667 4.666.667 5.666.667 4.666.667 5.000.000 5.091.667 16 p 5.857.143 6.000.000 6.142.857 5.857.143 5.571.429 5.571.429 6.142.857 5.260.368 [[alternative HTML version deleted]] ______________________________________________ R-help@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.