r - How to calculate the mode of all rows or columns from a dataframe -
I like how to calculate the mode of all the rows or columns from the matrix.
For example, I have:
seq & lt; - c (1,2,3) seq1 & lt; - Representative (seq, every = 4) mat1 < - Matrix (seq1, 3) mat1 rows & lt; - c (1,2,3) column & lt; - c ("a", "b", "c", "d") colnames (mat1) & lt; - Column rownames (mat1) & lt; - rows mat1 Now, I want to calculate the mode of each row and column. Thanks in advance
modefunc & lt; - Function (X) {tabresult & lt; - tabulate (x) themode & lt; - (tabresult == Max (tabresult) if (amount (tabresult == max (tabresult) gt 1) themode & lt; - NA return (themode)} #rows apply (mat1, 1, modefunc) #columns applied (mat1, 2, modefunc)
Comments
Post a Comment