apply

Correct use of sapply with Anova on multiple subsets in R

删除回忆录丶 提交于 2020-02-03 02:07:20
问题 I am trying to run a two-way ANOVA on multiple subsets of a data frame without having to actually subset the data as this is in-efficient Example data: DF<-structure(list(Sample = c(666L, 676L, 686L, 667L, 677L, 687L, 822L, 832L, 842L, 824L, 834L, 844L), Time = c(300L, 300L, 300L, 300L, 300L, 300L, 400L, 400L, 400L, 400L, 400L, 400L), Ploidy = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2n", "3n"), class = "factor"), Tissue = c("muscle", "muscle", "muscle", "liver

柯里化、偏函数、反柯里化

不问归期 提交于 2020-01-31 15:18:35
柯里化 描述 柯里化算是特殊的偏函数,把一个多参数函数转换成多个单参数函数,也就是说把一个具有n个参数的函数转换成n个一元函数 示例 // 正常写法 function add ( a , b ) { return a + b } const resAdd = add ( 2 , 3 ) console . log ( resAdd ) // 5 // 柯里化 function currieAdd ( a ) { return function ( b ) { return a + b } } const resCurrie = currieAdd ( 2 ) ( 3 ) console . log ( resCurrie ) // 5 通用写法 上面的示例代码比较简单,如果有十几个参数呢?所以需要一个通用柯里化的写发 代码的关键点在于 闭包,调用柯里化函数(currie)返回另外一个函数(_myFn),通过闭包缓存真正执行运算的函数(fn)和参数(args) 通过返回的函数传递参数,并进行判断,如果参数已经传递够了,就执行函数(fn)并返回结果,如果参数还没传递完,则继续返回函数(_myFn)接收参数 // 柯里化一个函数 function currie ( fn ) { // 利用闭包缓存传递进来的参数 const args = [ ] return function _myFn

每日一练19:合并两个数组的n种办法

孤街浪徒 提交于 2020-01-29 08:03:17
var ar1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'], ar2 = ['A', 'B', 'C', 'D']; 1:concat console.log(ar1.concat(ar2)); 2:apply ar1.push.apply(ar1,ar2); console.log(ar1); 3:call ar1.push.call(ar1,...ar2); console.log(ar1); 4:push ar1.push(...ar2); console.log(ar1); 5:Array.prototype.push.apply Array.prototype.push.apply(ar1,ar2); console.log(ar1); 6: Array.prototype.push.call Array.prototype.push.call(ar1, ...ar2); console.log(ar1); 参考 Array.prototype.push() 、 合并两个数组的方法 来源: CSDN 作者: 崧哥的编程之路 链接: https://blog.csdn.net/weixin_41406727/article/details/103601447

Subset dataframe such that all values in each row are less than a certain value

Deadly 提交于 2020-01-28 03:01:11
问题 I have a dataframe with a dimension column and 4 value columns. How can I subset the column such that all 4 columns for each record are less than a given x? I know I could do this manually using subset and specifying the condition for each column, but is there a way to do it using maybe an apply function? Below is a sample dataframe. For example let's say the x was 0.7. In that case I would want to eliminate any rows where any column of that row is more than 0.7). zips ABC DEF GHI JKL 1 1 0.8

Imputation with column medians in R

别说谁变了你拦得住时间么 提交于 2020-01-25 21:40:17
问题 If I have a vector, for example vec <- c(3,4,5,NA) I can replace the NA with the median value of the other values in the vector with the following code: vec[which(is.na(vec))] <- median(vec, na.rm = T) However, if I have a matrix containing NAs, applying this same code across all columns of the matrix doesn't give me back a matrix, just returning the medians of each matrix column. mat <- matrix(c(1,NA,3,5,6,7,NA,3,4,NA,2,8), ncol = 3) apply(mat, 2, function(x) x[which(is.na(x))] <- median(x,

Imputation with column medians in R

亡梦爱人 提交于 2020-01-25 21:40:12
问题 If I have a vector, for example vec <- c(3,4,5,NA) I can replace the NA with the median value of the other values in the vector with the following code: vec[which(is.na(vec))] <- median(vec, na.rm = T) However, if I have a matrix containing NAs, applying this same code across all columns of the matrix doesn't give me back a matrix, just returning the medians of each matrix column. mat <- matrix(c(1,NA,3,5,6,7,NA,3,4,NA,2,8), ncol = 3) apply(mat, 2, function(x) x[which(is.na(x))] <- median(x,

Colnames passed through an apply function R

蹲街弑〆低调 提交于 2020-01-25 19:15:13
问题 I am trying to use apply and a user defined function on a data.frame. Inside the function I would like to use the column name (for a title of a plot), but apply seems to strips the column name and passes only a vector. MWE: trialData <- data.frame('a' = rnorm(100), 'b' = rnorm(100), 'c' = rnorm(100)) someData <- function(dataInput){ return(colnames(dataInput)) } dataOutput <- apply(trialData, 2, someData) print(dataOutput) returns NULL . Is there any way of accessing the column name inside

Colnames passed through an apply function R

走远了吗. 提交于 2020-01-25 19:14:58
问题 I am trying to use apply and a user defined function on a data.frame. Inside the function I would like to use the column name (for a title of a plot), but apply seems to strips the column name and passes only a vector. MWE: trialData <- data.frame('a' = rnorm(100), 'b' = rnorm(100), 'c' = rnorm(100)) someData <- function(dataInput){ return(colnames(dataInput)) } dataOutput <- apply(trialData, 2, someData) print(dataOutput) returns NULL . Is there any way of accessing the column name inside

Using apply function over the row margin with expectation of stacked results

社会主义新天地 提交于 2020-01-25 08:34:29
问题 What is going on with the apply function below? Why doesn't it produce the desired output? Is there a simple way to produce the desired output using the apply function? I feel like I'm missing something really fundamental in how the apply function is working. I think this basically comes down to the following lines in the apply function. Line #67: else length(ans <- unlist(ans, recursive = FALSE)) Line #83: array(ans, c(len.a%/%d2, d.ans)) But I'm still not really getting what's going on. I

Using apply function over the row margin with expectation of stacked results

喜欢而已 提交于 2020-01-25 08:34:25
问题 What is going on with the apply function below? Why doesn't it produce the desired output? Is there a simple way to produce the desired output using the apply function? I feel like I'm missing something really fundamental in how the apply function is working. I think this basically comes down to the following lines in the apply function. Line #67: else length(ans <- unlist(ans, recursive = FALSE)) Line #83: array(ans, c(len.a%/%d2, d.ans)) But I'm still not really getting what's going on. I