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
Post a Comment