"Apply()" Command for finding Summaries
on Rows / Cols
=======================================================
* The "ColMeans()"
and "RowSums()" command
are designed as quick alternatives to a more generalised command
"Apply()"
* The "apply()"
command enables one to apply a function to rows or columns of a matrix or a
dataframe
* The general form of the "Apply()" command is given in the following manner :
apply(X,margin,FUN,....)
In this command , the applicable MARGIN within the parameter is
either 1 or 2 where 1 is for the rows and 2 is for the columns applicable for
the dataframe
* One can replace the "FUN" part within the parameter
of the apply() function and one can also add additional instructions which
might be appropriate to the command / function that one is applying
* Example :
One might add the parameter "na.rm = TRUE " as an
instruction to the apply function .
> mat01
[,1] [,2] [,3] [,4]
[1,] 1
2 3 4
[2,] 5
6 7 8
[3,] 9
10 11 12
[4,] 13
14 15 16
> apply (mat01 , 1 , mean , na.rm = TRUE )
[1] 2.5 6.5 10.5 14.5
* In such a case , one can see that the row names of the
original dataframe are displayed as output .
* If the dataframe has no set row names , then one will see the
result as a vector of values .
> apply (fw , 1 , median , na.rm = TRUE )
2.5 6.5 10.5 14.5
No comments:
Post a Comment