July 24, 2018

Apply Family Functions in R

R Apply Functions (Loop Functions)

Apply a function to vectors, arrays, lists, matrices, or data frames.

apply(X, MARGIN, FUN, ...) # input is array/matrix
apply(xx, 1, sort)
apply(R_matrix, 2, mean, trim = .2)
apply(matrix2, c(1,2), sum)
apply(y, 1, plot)
apply(cnt, 2, max, na.rm=TRUE)
apply(matrix.R, 2, function(x) length(x[x<0]))

lapply(X, FUN, ...) # list apply, input is vector/dataframe/list & returns list
lapply(x, FUN = length) 
lapply(x, quantile, probs = 1:3/4)
lapply(vector_R, mean)
lapply(df, sum)
lapply(mylist, "[", , 2) # extract second column of each list element
lapply(df1[,1:2], sum)
lapply(ycols, function(y) { ggplot(df, aes_string('x',y)) + geom_line() + ylab(y) } )
data[] <- lapply(data, gsub, pattern = " ", replacement = "", fixed = TRUE)
data <- do.call("rbind", lapply(data.R, as.data.frame))
titanic_train[ , col_names] <- lapply(columns, factor)
unlist(lapply(1:5, function(x) x^2)) # will return vector

sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) # simple apply, input is vector & returns vector/matrix/list
sapply(combi_df, mean)
vect <- sapply(df, max)
sapply(bollywood[4:7], max, na.rm=T)
sapply(marks, function(x) x/30*100)
sapply(data.frame.R, FUN = coef)
sapply(1:3, function(x) x^2, simplify=F) # will return list
sapply(airquality, mean, na.rm = TRUE)
bank2 <- data.frame(sapply(bank, factor))
(a2 <- sapply(v, f2, y = 2*(1:5), simplify = "array"))
sapply(bollywood[4:7], function(x) bollywood[which.max(x), 1])
h.f <- sapply(kernels, function(k)density(kernel = k, give.Rkern = TRUE))
df$NewCol <- sapply(df$X2, function(x) sum(x) + 2)
sapply(customer, function(x) length(which(x == "")))
companies[, sapply(companies, is.character)] <- sapply(companies[, sapply(companies, is.character)], iconv, "ISO-8859-1", "Shift_JIS", "UTF-8")

vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE) # vector/verified apply
vapply(x, FUN = length, FUN.VALUE = 0L)
a.2 <- vapply(R_vector, f2, outer(1:3, 1:5), y = 2*(1:5))
vapply(data.frame9, function(x) length(unique(x)) > 1, logical(1L)) # to check how many unique values there are in each column

tapply(X, INDEX, FUN = NULL, ..., default = NA, simplify = TRUE) # tagged apply
tapply(groups, groups, length)
tapply(1:n, fac, sum)
tapply(1:n, fac, length, default = 0)
tapply(x, y, sum)  
ct <- tapply(iris$Sepal.Width , iris$Species, summary )
tapply(presidents, cycle(presidents), mean, na.rm=T)
tapply(swiss$Fertility, list(mcut2, mcut3), mean)

mapply(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE,USE.NAMES = TRUE) # multivariate apply
mapply(rep, 1:4, 4:1)
mapply(rep, times = 1:4, MoreArgs = list(x = 42))
Map(sum, 1:5, 1:5, 1:5)
mapply(FUN=pmax, A, B)

eapply(env, FUN, ..., all.names = FALSE, USE.NAMES = TRUE) # each entry apply
eapply(env, quantile)
unlist(eapply(env, mean, USE.NAMES = FALSE))
> eapply(.GlobalEnv, is.function)

rapply(l, myFun) # recursive apply , for nested lists
rapply(X, function(x) x, how = "replace")
rapply(X, nchar, classes = "character", how = "unlist")
rapply(X, log, classes = "numeric", how = "replace", base = 2)
rapply(object, f, classes = "ANY", deflt = NULL, how = c("unlist", "replace", "list"), ...)

dendrapply(X, FUN, ...) # Dendrogram apply
dendrapply(dhc21, function(n) utils::str(attributes(n)))
dL <- dendrapply(dhc21, colLab)

replicate(n, expr, simplify = "array")
simplify2array(x, higher = TRUE)

Related Articles:  R Pipe operator (%>%)      Random Distributions in R

No comments:

Post a Comment