apply

Apply function to each cell in DataFrame

对着背影说爱祢 提交于 2019-11-30 02:39:24
I have a dataframe that may look like this: A B C foo bar foo bar bar foo foo bar I want to look through every element of each row (or every element of each column) and apply the following function to get the subsequent DF: def foo_bar(x): return x.replace('foo', 'wow') A B C wow bar wow bar bar wow wow bar Is there a simple one-liner that can apply a function to each cell? This is a simplistic example so there may be an easier way to execute this specific example other than applying a function, but what I am really asking about is how to apply a function in every cell within a dataframe.

call、apply、bind

随声附和 提交于 2019-11-29 23:57:17
call: 语法: 1 fun.call(thisArg[, arg1[, arg2[, ...]]]) thisArg:fun函数运行时指定的this值,可能的值为: 不传,或者传null,undefined, this指向 window对象 传递另一个函数的函数名fun2,this指向 函数fun2的引用 值为原始值(数字,字符串,布尔值),this会指向该原始值的自动包装对象,如 String、Number、Boolean 传递一个对象,函数中的this指向这个对象; 例如: 1 2 3 4 5 6 function a(){ console.log(this); } function b(){} a.call(b); // function b(){} 经常会看到这种使用情况: 1 2 3 4 5 function list() { // 将arguments转成数组 return Array.prototype.slice.call(arguments); } list(1,2,3); // [1, 2, 3] 为什么能实现这样的功能将arguments转成数组?首先call了之后,this指向了所传进去的arguments。我们可以假设slice方法的内部实现是这样子的:创建一个新数组,然后for循环遍历this,将this[i]一个个地赋值给新数组,最后返回该新数组

Scala case class private constructor but public apply method

流过昼夜 提交于 2019-11-29 22:55:43
If I have the following case class with a private constructor and I can not access the apply-method in the companion object. case class Meter private (m: Int) val m = Meter(10) // constructor Meter in class Meter cannot be accessed... Is there a way to use a case class with a private constructor but keep the generated apply-method in the companion public? I am aware that there is no difference (in my example) between the two options: val m1 = new Meter(10) val m2 = Meter(10) but I want to forbid the first option. -- edit -- Surprisingly the following works (but is not really what i want): val

How to apply function over each matrix element's indices

淺唱寂寞╮ 提交于 2019-11-29 20:20:24
I am wondering if there is a built-in function in R which applies a function to each element of the matrix (of course, the function should be computed based on matrix indices). The equivalent would be something like this: matrix_apply <- function(m, f) { m2 <- m for (r in seq(nrow(m2))) for (c in seq(ncol(m2))) m2[[r, c]] <- f(r, c) return(m2) } If there is no such built-in function, what is the best way to initialize a matrix to contain values obtained by computing an arbitrary function which has matrix indices as parameters? I suspect you want outer : > mat <- matrix(NA, nrow=5, ncol=3) >

R: apply a function to every element of two variables respectively

旧城冷巷雨未停 提交于 2019-11-29 19:56:30
问题 I have a function with two variables x and y: fun1 <- function(x,y) { z <- x+y return(z) } The function work fine by itself: fun1(15,20) But when I try to use it with two vectors for x and y with an apply function I do not get the correct 56*121 array Lx <- c(1:56) Ly <- c(1:121) mapply(fun1, Lx, Ly) I would be grateful for your help and also on advice on the fastest solution (eg is a data.table or dplyr solution faster than apply). 回答1: If you want to use mapply() you have to provide it with

What's the difference between a regular push and an Array.prototype.push.apply

≯℡__Kan透↙ 提交于 2019-11-29 19:06:58
问题 I don't quite understand the difference between the following two lines of code. In my code, the line with "apply" works the way I want it to, and the line with just regular push doesn't. So what is really going on when both of these are executed: //this one does not work the way i want it to $scope.items.push(result.data.stuff) //this one works! Array.prototype.push.apply($scope.items, result.data.stuff); Edit: sorry for confusion, I fixed it so that it has the "push" method in there 回答1:

区别和详解:js中call()和apply()的用法

混江龙づ霸主 提交于 2019-11-29 18:44:27
1、关于call()和apply()的疑点: apply和call的区别在哪里 什么情况下用apply,什么情况下用call apply的其他巧妙用法(一般在什么情况下可以使用apply) 2、语法和参数分析: apply和call都能继承另外一个对象的方法和属性; Function.apply(obj,args)方法能接收两个参数 obj:这个对象将代替Function类里this对象 args:这个是数组,它将作为参数传给Function(args-->arguments) call:和apply的意思一样,只不过是参数列表不一样. Function.call(obj,[param1[,param2[,…[,paramN]]]]) obj:这个对象将代替Function类里this对象 params:这个是一个参数列表 3、call()和apply()示例展示: /*定义一个Person类*/ function Person(name,age) { this.name=name; this.age=age; } /*定义一个学生类*/ function Student(name,age,grade) { //Person.apply(this,arguments);//特点:this指代student对象,只接收2个参数,arguments为隐式类数组对象,用来接收传入的参数;

js中的this和apply

 ̄綄美尐妖づ 提交于 2019-11-29 18:44:10
this是js的一个关键字,随着函数使用场合不同,this的值会发生变化。但是总有一个原则,那就是this指的是调用函数的那个对象。 1、纯粹函数调用。 function test() { this.x = 1; alert(x);}test(); 其实这里的this就是全局变量。看下面的例子就能很好的理解其实this就是全局对象Global。 var x = 1;function test() { alert(this.x);}test();//1var x = 1;function test() { this.x = 0;}test();alert(x);//0 2、作为方法调用,那么this就是指这个上级对象。 function test() { alert(this.x);}var o = {};o.x = 1;o.m = test;o.m(); //1 3、作为构造函数调用。所谓构造函数,就是生成一个新的对象。这时,这个this就是指这个对象。 function test() { this.x = 1;}var o = new test();alert(o.x);//1 4、apply调用 this指向的是apply中的第一个参数。 var x = 0;function test() { alert(this.x);}var o = {};o.x = 1;o.m =

Matrix multiplication in scheme, List of lists

折月煮酒 提交于 2019-11-29 16:37:51
I started to study Scheme and I do not understand some of it. I'm using DrRacket. I wrote the following code: (define mult_mat (λ (A B) (Trans_Mat (map (λ (x) (mul_Mat_vec A x)) (Trans_Mat B))))) That uses this functions: (define Trans_Mat (λ (A) (apply map (cons list A)))) (define mul_Mat_vec (λ (A v) (map (λ (x) (apply + (map * x v))) A))) In mult_mat , I multiply the matrix A in each vector of the transpose matrix B. It works fine. I found a code on the web that makes the multiplication in a way that I don't understand: (define (matrix-multiply matrix1 matrix2) (map (λ (row) (apply map (λ

How to use R for same field aggregation by multiple separate group

淺唱寂寞╮ 提交于 2019-11-29 16:00:01
I'm trying to perform count of an indicator on several (actually hundreds) groups separately (NOT on all combinations of all groups). I'll demonstrate it by simplified example: Assume I have that dataset data<-cbind(c(1,1,1,2,2,2) ,c(1,1,2,2,2,3) ,c(3,2,1,2,2,3)) > data [,1] [,2] [,3] [1,] 1 1 3 [2,] 1 1 2 [3,] 1 2 1 [4,] 2 2 2 [5,] 2 2 2 [6,] 2 3 3 and an indicator some_indicator<-c(1,0,0,1,0,1) then I want to run without loops (like apply by column) something like, aggregate(some_indicator,list(data[,1]),sum) aggregate(some_indicator,list(data[,2]),sum) aggregate(some_indicator,list(data[,3]