apply

call,apply,bind初识

◇◆丶佛笑我妖孽 提交于 2019-12-27 17:46:29
call、apply、bind三者是Function对象自带的方法   作用: 改变this指向 返回值: call、apply 是返回一个立即执行函数 bind返回一个函数,也便于稍后调用 使用方法: call与apply两者作用完全相同,不同的是接收参数的方式不太一样。call是将参数按顺序传递进去, 而apply将是把参数放在数组里。 bind()方法会创建一个新的函数,称之为绑定函数。当调用这个绑定函数的时候,绑定函数会以创建它时 传入bind()方法的第一个参数作为this,传入bind()方法的第二个以及以后的参数加上绑定函数运行时 本身的参数按照顺序作为原函数的参数来调用原函数。 call(this 要指向的对象(想指定的上下文),参数1,参数2,参数3,参数4,......,参数n) apply(this 要指向的对象(想指定的上下文),[参数1,参数2,参数3,参数4,......,参数n]) 适用场景: JavaScript 中,某个函数的参数数量是不固定的,因此要说适用条件的话,当你的参数是明确知道数量时用 call 。   而不确定的时候用 apply,然后把参数 push 进数组传递进去。当参数数量不确定时,函数内部也可以通过 arguments 这个数组来遍历所有的参数。 常见实例: (一)call   (1):数组之间追加 1 var array1 =

Remove columns from dataframe where ALL values are NA

不打扰是莪最后的温柔 提交于 2019-12-27 12:39:00
问题 I'm having trouble with a data frame and couldn't really resolve that issue myself: The dataframe has arbitrary properties as columns and each row represents one data set . The question is: How to get rid of columns where for ALL rows the value is NA ? 回答1: Try this: df <- df[,colSums(is.na(df))<nrow(df)] 回答2: The two approaches offered thus far fail with large data sets as (amongst other memory issues) they create is.na(df) , which will be an object the same size as df . Here are two

Remove columns from dataframe where ALL values are NA

眉间皱痕 提交于 2019-12-27 12:38:41
问题 I'm having trouble with a data frame and couldn't really resolve that issue myself: The dataframe has arbitrary properties as columns and each row represents one data set . The question is: How to get rid of columns where for ALL rows the value is NA ? 回答1: Try this: df <- df[,colSums(is.na(df))<nrow(df)] 回答2: The two approaches offered thus far fail with large data sets as (amongst other memory issues) they create is.na(df) , which will be an object the same size as df . Here are two

Last Observation Carried Forward In a data frame? [duplicate]

好久不见. 提交于 2019-12-27 12:05:03
问题 This question already has answers here : Replacing NAs with latest non-NA value (15 answers) Closed last year . I wish to implement a "Last Observation Carried Forward" for a data set I am working on which has missing values at the end of it. Here is a simple code to do it (question after it): LOCF <- function(x) { # Last Observation Carried Forward (for a left to right series) LOCF <- max(which(!is.na(x))) # the location of the Last Observation to Carry Forward x[LOCF:length(x)] <- x[LOCF]

理解js中的this指向以及call,apply,bind方法

懵懂的女人 提交于 2019-12-27 10:02:58
<script> function a(){ var user = "追梦子"; console.log(this.user); //undefined console.log(this); //Window } a(); </script> 按照我们上面说的 函数中的 this 指向的是 最终 调用 并执行 它的对象 ,(切记 this 指向的是最终执行该函数的对象) 这里的函数 a 实际是被 Window 对象所点出来的, Window 对象表示的是浏览器打开的窗口。例如上面的 Window 指的是这个窗口 file:///C:/Users/yanyanshan/Desktop/test/parent.html 。 下面的代码就可 等价于上面的代码 。 alert 也是 window 的一个属性,也是 window 点出来的。 <script> function a(){ var user = "追梦子"; console.log(this.user); //undefined console.log(this); //Window} window.a(); window.alert(“追门子”); </script> 再看一个例子 var o = { user:"追梦子", fn:function(){ console.log(this.user); //追梦子 } } o

How to use apply function instead of for loop if you have multiple if conditions to be excecuted

可紊 提交于 2019-12-25 11:47:15
问题 1st DF: t.d V1 V2 V3 V4 1 1 6 11 16 2 2 7 12 17 3 3 8 13 18 4 4 9 14 19 5 5 10 15 20 names(t.d) <- c("ID","A","B","C") t.d$FinalTime <- c("7/30/2009 08:18:35","9/30/2009 19:18:35","11/30/2009 21:18:35","13/30/2009 20:18:35","15/30/2009 04:18:35") t.d$InitTime <- c("6/30/2009 9:18:35","6/30/2009 9:18:35","6/30/2009 9:18:35","6/30/2009 9:18:35","6/30/2009 9:18:35") >t.d ID A B C FinalTime InitTime 1 1 6 11 16 7/30/2009 08:18:35 6/30/2009 9:18:35 2 2 7 12 17 9/30/2009 19:18:35 6/30/2009 9:18:35

R all possible combinations

谁说胖子不能爱 提交于 2019-12-25 08:33:53
问题 I have fallowing data frame: > my.df x y 1 0.4597406 0.8439140 2 0.4579697 0.7461805 3 0.5593259 0.6646701 4 0.3607346 0.7792931 5 0.8377520 1.0445919 6 0.5597406 1.0445919 I want to create all possible combinations > my.df x y 1 0.4597406 0.8439140 2 0.4597406 0.7461805 3 0.4597406 0.6646701 4 0.4597406 0.7792931 5 0.4597406 1.0445919 6 0.4597406 1.0445919 7 0.4579697 0.8439140 8 0.4579697 0.7461805 9 0.4579697 0.6646701 ... (Not all the combinations are showing here - This is to show the

R - return boolean if any strings in a vector appear in any of several columns

别来无恙 提交于 2019-12-25 07:54:09
问题 I have a large data frame, each row of which refers to an admission to hospital. Each admission is accompanied by up to 20 diagnosis codes in columns 5 to 24. Col1 Col2 Col3 Col4 Diag_1 Diag_2 Diag_3 ... Diag_20 data data data data J123 F456 H789 E468 data data data data T452 NA NA NA Separately, I have a vector ( risk_codes ) of length 136, all strings. These strings are risk codes that can be similar to the truncated diagnosis codes (e.g. J12 would be ok, F4 would be ok, H798 would not). I

Vectorizing a loop through lines of data frame R while accessing multiple variables the dataframe

不问归期 提交于 2019-12-25 07:49:14
问题 Yet another apply question. I've reviewed a lot of documentation on the apply family of functions in R (and use them quite a bit in my work). I've defined a function myfun below which I want to apply to every row of the dataframe inc . I think I need some variant of apply(inc,1,myfun) I've played around with it for a while, but still can't quite get it. I've included a loop which achieves exactly what I want to do... it's just super slow and inefficient on my real data which is considerably

How to calculate means across rows of three multi-column dataframes?

不打扰是莪最后的温柔 提交于 2019-12-25 04:30:52
问题 Let's say I have 3 data frames, each a 5x5 object as such: set.seed(1) x <-as.data.frame(matrix(rnorm(10),ncol=5,nrow=5)) colnames(x) <-c("a","b","c","d","e") y <-as.data.frame(matrix(rnorm(10),ncol=5,nrow=5)) colnames(y) <-c("f","g","h","i","j") z <-as.data.frame(matrix(rnorm(10),ncol=5,nrow=5)) colnames(z) <-c("k","l","m","n","o") So, x, for instance, looks like: > x a b c d e 1 -0.6264538 -0.8204684 -0.6264538 -0.8204684 -0.6264538 2 0.1836433 0.4874291 0.1836433 0.4874291 0.1836433 3 -0