Last data update: 2014.03.03

R: Unifies all same X observations
averageSameXsR Documentation

Unifies all same X observations

Description

The function unifies all the observations with the same X values into one obseravtion with regard to the weights of each observation

Usage

averageSameXs(mat)

Arguments

mat

A matrix containing the X,Y,Weight columns after the calcWeights function been used on it

Value

Data set with only row for each X value

Author(s)

Tal Carmi, Liat Gaziel

Examples

d <- c(1,1,3,4)
e <- c(5,6,7,8)
f <- c(1,1,1,1)
mydata <- data.frame(d,e,f)
names(mydata) <- c("X","Y","Weight")
averageSameXs(mydata)

## The function is currently defined as
function (mat) 
{
    n <- size(mat)
    temp <- matrix(NA, nrow = n, ncol = 3)
    line <- 1
    i <- 1
    while (i <= n) {
        j <- i
        sumY <- 0
        sumWeights <- 0
        while (j <= n & mat[i, 1] == mat[j, 1]) {
            sumY <- sumY + mat[j, 2] * mat[j, 3]
            sumWeights <- sumWeights + mat[j, 3]
            j <- j + 1
        }
        temp[line, 1] <- mat[i, 1]
        temp[line, 2] <- sumY/sumWeights
        temp[line, 3] <- sumWeights
        line <- line + 1
        i <- j
    }
    temp <- as.data.frame(temp[1:(line - 1), ])
    names(temp)[1] = "X"
    names(temp)[2] = "Y"
    names(temp)[3] = "Weights"
    return(temp)
  }

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(ACCLMA)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/ACCLMA/averageSameXs.Rd_%03d_medium.png", width=480, height=480)
> ### Name: averageSameXs
> ### Title: Unifies all same X observations
> ### Aliases: averageSameXs
> ### Keywords: ~kwd1 ~kwd2
> 
> ### ** Examples
> 
> d <- c(1,1,3,4)
> e <- c(5,6,7,8)
> f <- c(1,1,1,1)
> mydata <- data.frame(d,e,f)
> names(mydata) <- c("X","Y","Weight")
> averageSameXs(mydata)
  X   Y Weights
1 1 5.5       2
2 3 7.0       1
3 4 8.0       1
> 
> ## The function is currently defined as
> function (mat) 
+ {
+     n <- size(mat)
+     temp <- matrix(NA, nrow = n, ncol = 3)
+     line <- 1
+     i <- 1
+     while (i <= n) {
+         j <- i
+         sumY <- 0
+         sumWeights <- 0
+         while (j <= n & mat[i, 1] == mat[j, 1]) {
+             sumY <- sumY + mat[j, 2] * mat[j, 3]
+             sumWeights <- sumWeights + mat[j, 3]
+             j <- j + 1
+         }
+         temp[line, 1] <- mat[i, 1]
+         temp[line, 2] <- sumY/sumWeights
+         temp[line, 3] <- sumWeights
+         line <- line + 1
+         i <- j
+     }
+     temp <- as.data.frame(temp[1:(line - 1), ])
+     names(temp)[1] = "X"
+     names(temp)[2] = "Y"
+     names(temp)[3] = "Weights"
+     return(temp)
+   }
function (mat) 
{
    n <- size(mat)
    temp <- matrix(NA, nrow = n, ncol = 3)
    line <- 1
    i <- 1
    while (i <= n) {
        j <- i
        sumY <- 0
        sumWeights <- 0
        while (j <= n & mat[i, 1] == mat[j, 1]) {
            sumY <- sumY + mat[j, 2] * mat[j, 3]
            sumWeights <- sumWeights + mat[j, 3]
            j <- j + 1
        }
        temp[line, 1] <- mat[i, 1]
        temp[line, 2] <- sumY/sumWeights
        temp[line, 3] <- sumWeights
        line <- line + 1
        i <- j
    }
    temp <- as.data.frame(temp[1:(line - 1), ])
    names(temp)[1] = "X"
    names(temp)[2] = "Y"
    names(temp)[3] = "Weights"
    return(temp)
}
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>