Re: [R] mean calculation within dataframe corrupted

From: Joshua Wiley <jwiley.psych_at_gmail.com>
Date: Tue, 01 Jun 2010 22:51:16 -0700

Hello,

I was hunting for something else and came across your old message, and I noticed that it had never received an answer (at least on the list).  I really tried to figure out what might be going on just by looking at your code, but I could not get enough information from your email and your code is not reproducible. For instance, in your function, you reference other functions that are not part of base R (e.g., calculateGroupMean() ). Also, your data, plan1, plan2, etc. and u_proj. If you still have a question about your function, please provide a reproducible example.

Best regards,

Josh

On Wed, Feb 10, 2010 at 4:06 PM, Chaehan So <chaehan.so_at_gmail.com> wrote:
> 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_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.
>

-- 
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/

______________________________________________
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.
Received on Wed 02 Jun 2010 - 05:54:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 02 Jun 2010 - 07:40:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive