fn = function(x1, x2, x3) { return(x1^2 + x2 * x1 + x3) } 我们将x1作为'data'中的每个值,将x2,x3作为其他参数,这些参数将首先声明,然后通过apply函数传递: 代码语言:javascript 复制 b = 2 c = 1 # apply along each row: row_fn <- apply(data, 1, fn, x2 = b, x3 = c) #...
> # Apply given R function to each row > ore.rowApply(IRIS_TABLE, + function(dat) { + # Any R code goes here. Operates on one row of IRIS_TABLE at + # a time + cbind(dat, dat$Petal.Length) + }) $`1` Sepal.Length Sepal.Width Petal.Length Petal.Width Species dat$Petal.Len...
> myScale=function(x){ + x.Mean=apply(x,2,mean) + x.sd=apply(x,2,sd) + #注意下面...
We apply the length function to each pile, giving us the count in each pile, exactly what we needed.Tip: In coding, certain patterns do arise often, one did here. In fact, there are coding books with "design patterns" in their titles. Take note when you see the same pattern a lot....
delete_row <- function(data, condition) { if (is.data.frame(data) || is.matrix(data)) { if (is.function(condition)) { rows_to_delete <- apply(data, 1, condition) } else { rows_to_delete <- eval(condition, data) } data <- data[!rows_to_delete, ] ...
# apply family system.time({ myfunc <- function(x) { if ((x['col1'] + x['col2'] + x['col3'] + x['col4']) > 4) { "greater_than_4" } else { "lesser_than_4" } } output <- apply(df[, c(1:4)], 1, FUN=myfunc) # apply 'myfunc' on every row ...
用R写循环从低到高有三种境界:手动 for 循环,apply 函数族,purrr 包泛函式编程。 map(.x, .f, …): Apply a function to each element of a list or vector. map(x, is.logical) map2(.x, .y, .f, …): Apply a function to pairs of elements from two lists, vectors. map2(x, y, sum...
EGene <- EGene[order(unlist(lapply(EGene,function(x){sum(exp_TPM[x,])})),decreasing = T)][1:10] exprSet <- t(scale(t(log2(exp_TPM+0.01))) OwnScore <- apply(exprSet[MGene,],2,sum) - apply(exprSet[EGene,],2,sum) cor...
This example shows how to return a matrix of row numbers using the row function. Have a look at the following R code: row(my_mat)# Apply row function The output of the previously shown R code is shown in Table 2 – A matrix object that contains the row numbers for each column of ...
{rrapply}: Revisiting R-base rapply() The minimal {rrapply}-package contains a single function rrapply(), providing an extended implementation of R-base’s rapply() function, which applies a function f to all elements of a nested list recursively and controls how to structure the returned...