r - How to calculate Mode (Statistics) for a set of every 10 numbers in a large data set -


like if have 1223455567 1777666666 want output 5 6 . how can in r language?

i know how find mean every 10 data want mode.

here tried mean

mean10 <- aggregate(level, list(rep(1:(nrow(level) %/% n+1),each = n, len = nrow(level))), mean)[-1];

and there function mode follow:

mode <- function(dataframe){     df <- as.data.frame(dataframe)      mode2 <- function(x){           if (is.numeric(x) == false){         df <- as.data.frame(table(x))           df <- df[order(df$freq), ]                  m <- max(df$freq)                 mode1 <- as.vector(as.character(subset(df, freq == m)[, 1]))          if (sum(df$freq)/length(df$freq)==1){             warning("no mode: frequency of values 1", call. = false)         }else{             return(mode1)         }      }else{          df <- as.data.frame(table(x))           df <- df[order(df$freq), ]                  m <- max(df$freq)                 mode1 <- as.vector(as.numeric(as.character(subset(df, freq == m)[, 1])))          if (sum(df$freq)/length(df$freq)==1){             warning("no mode: frequency of values 1", call. = false)         }else{             return(mode1)         }     } }  return(as.vector(lapply(df, mode2))) } 

you can use zoo package calculate moving mode:

library(zoo)  # sample data d <- data.frame(x = sample(1:3, 100, t))  # mode function (handles ties choosing one) my_mode <- function(x) as.numeric(which.max(table(x)))  # add moving mode new variable transform(d, moving_mode = rollapply(x, 10, fun = my_mode, fill = na)) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -