apply

call和apply

别说谁变了你拦得住时间么 提交于 2020-01-15 05:45:11
call和apply的作用: 1、调用函数 2、改变所调用函数的内部的this指向 3、借用别的对象的方法 */ function foo(a,b,c){ console.log(a,b,c); console.log(a + b + c); console.log(this); } // foo(1,2,3); // call的参数是单个的 foo.call(null,1,2,3); // // apply的参数是一个数组 // foo.apply(null,[1,2,3]); // 在全局作用域内,所有的全局变量和全局函数都是window的成员(属性或者方法) var a = 1,b = 2; function fn(c,d){ console.log(this.a + this.b + c + d); } fn(3,4); //10 fn.call({ a : 3, b : 4 },5,6); //18 fn.apply({ a : 6, b : 9 },[7,8]); //30 var arr = [12,324,45,456,56,6767,34]; console.log(Math.max.apply(null,arr)); //6767 来源: https://www.cnblogs.com/woniubushinide/p/6791827.html

Method for calculating distance between all points in a dataframe containing a list of xy coordinates

最后都变了- 提交于 2020-01-14 10:33:24
问题 I'm sure this has been answered before, but I can't find the thread for the life of me! I am trying to use r to produce a list of all the distances between pairs of xy coordinates in a dataframe. The data is stored something like this: ID = c('1','2','3','4','5','6','7') x = c(1,2,4,5,1,3,1) y = c(3,5,6,3,1,5,1) df= data.frame(ID,x,y) At the moment I can calculate the distance between two points using: length = sqrt((x1 - x2)^2+(y1 - y2)^2). However, I am uncertain as to where to go next.

Method for calculating distance between all points in a dataframe containing a list of xy coordinates

会有一股神秘感。 提交于 2020-01-14 10:32:30
问题 I'm sure this has been answered before, but I can't find the thread for the life of me! I am trying to use r to produce a list of all the distances between pairs of xy coordinates in a dataframe. The data is stored something like this: ID = c('1','2','3','4','5','6','7') x = c(1,2,4,5,1,3,1) y = c(3,5,6,3,1,5,1) df= data.frame(ID,x,y) At the moment I can calculate the distance between two points using: length = sqrt((x1 - x2)^2+(y1 - y2)^2). However, I am uncertain as to where to go next.

Why does apply with too many arguments throw “Maximum call stack size exceeded”?

删除回忆录丶 提交于 2020-01-14 07:50:07
问题 In Chrome and Node, the following code throws an error: function noop() {} var a = new Array(1e6) // Array[1000000] noop.apply(null, a) // Uncaught RangeError: Maximum call stack size exceeded I understand why it might be a Bad Idea to pass 1 million arguments to a function, but can anyone explain why the error is Maximum call stack size exceeded instead of something more relevant? (In case this seems frivolous, the original case was Math.max.apply(Math, lotsOfNumbers) , which is a not

apply() and forceAndCall() ignoring get() from parent.frame()

女生的网名这么多〃 提交于 2020-01-13 05:39:13
问题 I don't understand why this code is failing on the last printed line: f <- function(x) get('v', envir = parent.frame(), inherits = TRUE) run <- function() { v <- 'test variable' print(f()) print((function() f())()) print(apply(X = data.frame(1:2), MARGIN = 1, f)) } run() with: Error in get("v", envir = parent.frame(), inherits = TRUE) : object 'v' not found The first print statement shows that v is found in parent.frame(1) . The second print statement shows that v is found in parent.frame(2)

Losing Class information when I use apply in R

跟風遠走 提交于 2020-01-12 06:03:47
问题 When I pass a row of a data frame to a function using apply, I lose the class information of the elements of that row. They all turn into 'character'. The following is a simple example. I want to add a couple of years to the 3 stooges ages. When I try to add 2 a value that had been numeric R says "non-numeric argument to binary operator." How do I avoid this? age = c(20, 30, 50) who = c("Larry", "Curly", "Mo") df = data.frame(who, age) colnames(df) <- c( '_who_', '_age_') dfunc <- function

Losing Class information when I use apply in R

心不动则不痛 提交于 2020-01-12 06:03:33
问题 When I pass a row of a data frame to a function using apply, I lose the class information of the elements of that row. They all turn into 'character'. The following is a simple example. I want to add a couple of years to the 3 stooges ages. When I try to add 2 a value that had been numeric R says "non-numeric argument to binary operator." How do I avoid this? age = c(20, 30, 50) who = c("Larry", "Curly", "Mo") df = data.frame(who, age) colnames(df) <- c( '_who_', '_age_') dfunc <- function

R use apply function on xts zoo class

狂风中的少年 提交于 2020-01-11 13:37:09
问题 I am new in R and I try to use apply function on the xts zoo class, however it shows error. I have a formula: ((2*Close-High-Low)/(High-Low)) * Volume Input: y <- getSymbols("0005.HK", auto.assign = FALSE, src = "yahoo") Error: y$II <- apply(y,2,function(x) (2Cl(x) - Hi(x) - Lo(x)) / ((Hi(x) - Lo(x)) * Vo(stk))) Error: unexpected symbol in "apply(y,2,function(x) (2Cl" and then I tried another one: Error: y$II <- apply(y,2,function(x) (2(x[,4]) - x[,2] - x[,3]) / (x[,2] - x[,3]) * x[,5]) Error

how to create many linear models at once and put the coefficients into a new matrix?

╄→гoц情女王★ 提交于 2020-01-11 07:51:44
问题 I have 365 columns. In each column I have 60 values. I need to know the rate of change over time for each column (slope or linear coefficient). I created a generic column as a series of numbers from 1:60 to represent the 60 corresponding time intervals. I want to create 356 linear regression models using the generic time stamp column with each of the 365 columns of data. In other words, I have many columns and I would like to create many linear regression models at once, extract the

Perform pairwise comparison of matrix

南楼画角 提交于 2020-01-10 20:18:51
问题 I have a matrix of n variables and I want to make an new matrix that is a pairwise difference of each vector, but not of itself. Here is an example of the data. Transportation.services Recreational.goods.and.vehicles Recreation.services Other.services 2.958003 -0.25983789 5.526694 2.8912009 2.857370 -0.03425164 5.312857 2.9698044 2.352275 0.30536569 4.596742 2.9190123 2.093233 0.65920773 4.192716 3.2567390 1.991406 0.92246531 3.963058 3.6298314 2.065791 1.06120930 3.692287 3.4422340 I tried