0

I have got a matrix of 96 rows and 42372 columns.

I am supposed to take the mode of first 4 rows in all columns and dump it as a first row in another matrix.

Then take mode of next four rows in all columns and dump it as a second row in another matrix.

And so on.

New matrix will be having 24 rows and 42372 columns.

I have written a function as below; SOURCE: Is there a built-in function for finding the mode?

GetMode <- function(x)

 {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

Output is the first matrix. (96 rows and 42372 columns)

Output2 is the new matrix. (24 rows and 42372 columns)

I am trying as of now as

output2[1,]<-GetMode(output[1:4,])

But it is printing mode for each row across all columns and printing it as a new row.

M--
  • 25,431
  • 8
  • 61
  • 93
Earthshaker
  • 549
  • 1
  • 7
  • 12

2 Answers2

0

Give this a shot and tell me if it works for you:

# Fake Data
output <- matrix(round(runif(96*42372, 1, 40)), nrow = 96, ncol = 42372)

# https://stackoverflow.com/questions/2547402/is-there-a-built-in-function-for-finding-the-mode
GetMode <- function(x){
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

every = 4
output2 <- output; output2[] <- NA
output2 <- sapply(seq(from = 1, to = nrow(output), by = every), 
                      function(i) apply(output[i:(i+(every-1)),], 2, GetMode))

dim(output2)
output2[1:6,1:6]
Evan Friedland
  • 3,062
  • 1
  • 11
  • 25
  • Hi Evan , Thank you very much . The code is working however after the new matrix is created i am transposing it to exhange no. of columns and rows. At that time some of the records are getting messed up and values are shifted from one column to another. Thanks a lot !! – Earthshaker Jun 20 '17 at 18:48
  • Try removing the t() function i have around the sapply function? To be clear, you want 24 columns and 42372 rows then? – Evan Friedland Jun 20 '17 at 18:50
  • Hi Evan , yes, my final product has to be 24 columns and 42372 rows. – Earthshaker Jun 20 '17 at 19:04
  • Okay just remove the t() function. output2 <- sapply(seq(from = 1, to = nrow(output), by = every), function(i) apply(output[i:(i+(every-1)),], 2, GetMode)) – Evan Friedland Jun 20 '17 at 19:06
0

You can use data.table package:

library(data.table)
#df0 is the original *"data.frame"*
df1 <- cbind(rep(1:(96/4), each=4), df0) #Add a column for grouping
colnames(df1)[1] <- "id"                 #Make the col name clean

df1 <- data.table(df1)                   #Convert to data table

df.mode <- df1[,GetMode(.SD),id]         #Get the mode for each group and all columns

Data:

df0 <- matrix(rep(1:24000, each = 4), nrow = 96, ncol = 100)
df0 <- data.frame(df0)

Function:

GetMode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}
M--
  • 25,431
  • 8
  • 61
  • 93