Skip to content

Use of Sappy and Tappy for Mathematical Calculation

4 messages · Rantony, arun, Nordlund, Dan (DSHS/RDA) +1 more

#
Hi,

i have a matrix like this,

ABC        XYZ    ......    .....   .....
-----         ------
12            20      ......    .....   .....
24            35      ......    .....   .....
30            40      ......    .....   .....

Here, i need to get 
                           Sum of each columns,
                           Mean of each columns, 
                           median of each columns,
                           mode of each columns, 
                           Standard deviation  of each columns, 
                           variance of each columns, 
                           range of each columns,
                           count of each columns,
                           max of each columns, 
                           min of each columns

Can i get output using sappy or tappy functions ? because there have alots
of records.

Could you please help me fast its kind of urgent !

- Thanks 
Antony 

--
View this message in context: http://r.789695.n4.nabble.com/Use-of-Sappy-and-Tappy-for-Mathematical-Calculation-tp4635969.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi,

?dat1<-data.frame(ABC=c(12,24,30),XYZ=c(20,35,40))
?dat2<-as.matrix(dat1)
?#mean
?dat2mean<-apply(dat2,2,mean)
?dat2mean
? # ? ABC????? XYZ 
#22.00000 31.66667 
dat2sum<-apply(dat2,2,sum)
?dat2median<-apply(dat2,2,median)
?dat2max<-apply(dat2,2,max)
dat2min<-apply(dat2,2,min)

?dat2range<-apply(dat2,2,range)

dat2count<-apply(dat2,2,length)
dat2sd<-apply(dat2,2,sd)
dat2var<-apply(dat2,2,var)

A.K.



----- Original Message -----
From: Rantony <antony.akkara at ge.com>
To: r-help at r-project.org
Cc: 
Sent: Tuesday, July 10, 2012 6:16 AM
Subject: [R] Use of Sappy and Tappy for Mathematical Calculation

Hi,

i have a matrix like this,

ABC? ? ? ? XYZ? ? ......? ? .....?  .....
-----? ? ? ?  ------
12? ? ? ? ? ? 20? ? ? ......? ? .....?  .....
24? ? ? ? ? ? 35? ? ? ......? ? .....?  .....
30? ? ? ? ? ? 40? ? ? ......? ? .....?  .....

Here, i need to get 
? ? ? ? ? ? ? ? ? ? ? ? ?  Sum of each columns,
? ? ? ? ? ? ? ? ? ? ? ? ?  Mean of each columns, 
? ? ? ? ? ? ? ? ? ? ? ? ?  median of each columns,
? ? ? ? ? ? ? ? ? ? ? ? ?  mode of each columns, 
? ? ? ? ? ? ? ? ? ? ? ? ?  Standard deviation? of each columns, 
? ? ? ? ? ? ? ? ? ? ? ? ?  variance of each columns, 
? ? ? ? ? ? ? ? ? ? ? ? ?  range of each columns,
? ? ? ? ? ? ? ? ? ? ? ? ?  count of each columns,
? ? ? ? ? ? ? ? ? ? ? ? ?  max of each columns, 
? ? ? ? ? ? ? ? ? ? ? ? ?  min of each columns

Can i get output using sappy or tappy functions ? because there have alots
of records.

Could you please help me fast its kind of urgent !

- Thanks 
Antony 

--
View this message in context: http://r.789695.n4.nabble.com/Use-of-Sappy-and-Tappy-for-Mathematical-Calculation-tp4635969.html
Sent from the R help mailing list archive at Nabble.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.
#
Here is some code to get you started.  You can add in the other functions that you want.  You will need to figure out what you want to do if there are missing values.  There are built-in functions for most everything you want.  You get the range from the min and the max, and you need to decide what to do if a variable has 2 or more modes (you  will also need to determine how you are going to get the mode).

 
# here is sample matrix
mat <- matrix(1:100,nrow=10)
colnames(mat) <- LETTERS[1:10]

# define summarize function
summarize <- function(m) {
  sums <- apply(m, 2, sum)
  counts <- apply(m, 2, length)
  means <- apply(m, 2, mean)
  return(rbind(sums, counts, means))
}

# summarize your matrix
summarize(mat)


Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
On Jul 10, 2012, at 11:30 AM, "Nordlund, Dan (DSHS/RDA)" <NordlDJ at dshs.wa.gov> wrote:

            
I like Dan's solution a lot and it teaches inportant idioms, but I might suggest that you use the col***() functions from the MatrixStats package if speed/data-size is of issue. 

Best,
Michael