apply

JavaScript call() and Prototype - Slice Function

此生再无相见时 提交于 2019-12-10 07:22:37
问题 I'm reading the MDN Article on slice in JavaScript. I understand everything except the 2nd example in the section titled Array-Like Objects . It says we can simplify the first example by making slice our own function as so: var unboundSlice = Array.prototype.slice; var slice = Function.prototype.call.bind(unboundSlice); function list() { return slice(arguments); } var list1 = list(1, 2, 3); // [1, 2, 3] What I don't understand is how call can come right after prototype on the second line. I

Apply many functions to one vector

非 Y 不嫁゛ 提交于 2019-12-10 04:33:42
问题 I am looking for an option to apply many functions to one vector. I think it is kind on an inverse apply function where one function is applied to many vectors (or columns). Is there a way of specifing two or more functions (like mean and max) and apply this on a vector? 回答1: Similar to @CathG's comment, but without get : v <- rnorm(10) funs <- list(mean, median, sd) sapply(funs, function(fun, x) fun(x), x = v) Or with do.call : sapply(funs, do.call, args = list(v)) 来源: https://stackoverflow

Scala: companion objects and “new” keyword

大兔子大兔子 提交于 2019-12-10 03:09:31
问题 In my recent posts about using or omitting a "new" keyword in Scala ( "new" keyword in Scala) I was told that the omission comes from the fact that certain classes have companion objects defined with apply method on them. My question is: are we able to tell or is there any general rule to distinguish which classes/objects have a companion object and apply method? Thanks in advance and sorry of it's a stupid question, but coming from a Java background it is a bit confusing. 回答1: In the Scala

Conditional calculating Maximum value in the column

人盡茶涼 提交于 2019-12-09 23:45:44
问题 I have the following table: Class x2 x3 x4 A 14 45 53 A 8 18 17 A 16 49 20 B 78 21 48 B 8 18 5 I need for each "Class" (A and B) find the maximum value in column "X3", keep that row and delete other rows. The output should be in format like: Class x2 x3 x4 A 14 49 20 B 78 21 48 Please, ask me questions if something unclear in my problem. Thank you! 回答1: A base R approach could be: mydf[as.logical(with(mydf, ave(x3, Class, FUN = function(x) x == max(x)))), ] # Class x2 x3 x4 # 3 A 16 49 20 # 4

Set column names while calling a function

匆匆过客 提交于 2019-12-09 21:10:05
问题 Consider we have a numeric data.frame foo and want to find the sum of each two columns: foo <- data.frame(x=1:5,y=4:8,z=10:14, w=8:4) bar <- combn(colnames(foo), 2, function(x) foo[,x[1]] + foo[,x[2]]) bar # [,1] [,2] [,3] [,4] [,5] [,6] #[1,] 5 11 9 14 12 18 #[2,] 7 13 9 16 12 18 #[3,] 9 15 9 18 12 18 #[4,] 11 17 9 20 12 18 #[5,] 13 19 9 22 12 18 Everything is fine, except the column names that are missing from bar . I want column names of bar to show the related columns in foo , for

Counting the number of rows of a series of csv files

本小妞迷上赌 提交于 2019-12-09 18:16:40
问题 I'm working through an R tutorial and suspect that I have to use one of these functions but I'm not sure which (Yes I researched them but until I become more fluent in R terminology they are quite confusing). In my working directory there is a folder "specdata". Specdata contains hundreds of CSV files named 001.csv - 300.csv. The function I am working on must count the total number of rows for an inputed number of csv files. So if the argument in the function is 1:10 and each of those files

apply call的区别

此生再无相见时 提交于 2019-12-09 15:17:37
apply() 和 call() 两者没有太大的区别 都是可以改变作用域 主要就是传的参数不同 apply() 必须要求传的是数组 call()要求一个一个单个的传 来源: 51CTO 作者: 喝醉的熊 链接: https://blog.51cto.com/13550695/2457045

using data.table with multiple threads in R

可紊 提交于 2019-12-09 12:46:18
问题 Is there a way to utilize multiple threads for computation using data.table in R? For example let's say i have the following data.table : dtb <- data.table(id=rep(1:10000, 1000), x=1:1e7) setkey(dtb, id) f <- function(m) { #some really complicated function } res <- dtb[,f(x), by=id] Is there a way to get R to multithread this if f takes a while to compute? What about in the case that f is quick, will multithreading help or is most of the time going to be taken by data.table in splitting

Rolling regression over multiple columns

拥有回忆 提交于 2019-12-09 11:57:48
问题 I have an issue finding the most efficient way to calculate a rolling linear regression over a xts object with multiple columns. I have searched and read several previously questions here on stackoverflow. This question and answer comes close but not enough in my opinion as I want to calculate multiple regressions with the dependent variable unchanged in all the regressions. I have tried to reproduce an example with random data: require(xts) require(RcppArmadillo) # Load libraries data <-

Calculate correlation by aggregating columns of data frame

断了今生、忘了曾经 提交于 2019-12-09 03:25:04
问题 I have the following data frame: y <- data.frame(group = letters[1:5], a = rnorm(5) , b = rnorm(5), c = rnorm(5), d = rnorm(5) ) How to get a data frame which gives me the correlation between columns a,b and c,d for each row? something like: sapply(y, function(x) {cor(x[2:3],x[4:5])}) Thank you, S 回答1: You could use apply > apply(y[,-1],1,function(x) cor(x[1:2],x[3:4])) [1] -1 -1 1 -1 1 Or ddply (although this might be overkill, and if two rows have the same group it will do the correlation