apply

Using ifelse Within apply

女生的网名这么多〃 提交于 2019-12-05 23:06:32
问题 I am trying to make a new column in my dataset give a single output for each and every row, depending on the inputs from pre-existing columns. In this output column, I desire "NA" if any of the input vales in a given row are "0". Otherwise (if none of the inputs are 0), I want the output for that row to be the number of unique values of the inputs. I thought that the solution would use an ifelse function nested within an apply function, but I get an error that I do not understand. data$output

find the column with lowest value in r

有些话、适合烂在心里 提交于 2019-12-05 18:27:33
I have large size matrix and try to find the column that has the minimum value for each row. For instance, here is my matrix, (simply generate with matrix(sample(12),nrow = 3) ). With the matrix I want to to have a vector (3,4,1) representing the column number which contains the lowest value in each row. How should I do it? It could be duplicated question but I could not find answers. [,1] [,2] [,3] [,4] [1,] 10 11 1 12 [2,] 8 9 7 3 [3,] 2 5 6 4 Use max.col : max.col(-mat) # [1] 3 4 1 来源: https://stackoverflow.com/questions/49203020/find-the-column-with-lowest-value-in-r

Error with xts::apply: “Error in coredata.xts(x) : currently unsupported data type”

二次信任 提交于 2019-12-05 18:24:31
The error occurred to me When I was trying to do the following work: # generate random integrals # data <- xts(floor(runif(100, 1,101)),as.Date("1973-02-01") + c(1:100) - 1) apply.monthly(data, diff,1,1) , while this one works: apply.monthly(data,mean) I have checked similar questions posted, but it seems they do not apply to the situation here. Any advice? Some further explanation: The reason I need this is that I got a time series data set like the following, 1990-05 100 1990-04 80 1990-03 60 1990-02 20 1990-01 5 1989-12 110 1989-11 89 1989-10 78 ... In each year, y(t)=y_(t-1)+dy , where dt

Python里的匿名函数(lambda)与apply(),filter() ,map(),reduce(),以及函数的可变长参数

南楼画角 提交于 2019-12-05 17:57:54
lambda: 提到 Lambda演算,更多时候是与函数式编程纠缠在一起的。这种设计思想讲究抛弃变量和状态,使用纯函数的递归系统来构建程序(个人理解)。虽然函数式编程与 Python 的面向对象背道而驰,但并不妨害 Python 借鉴其中某些有价值的内容。即是说,并不能因为 lambda 的存在就认为 Python 是一门函数式编程语言,它只是因为在某些细节上显得更有效率而被引入的。比如 Python 里用 lambda 来定义匿名函数,并与标题中提到的 apply() 等内建函数一起构建一些程序结构。 匿名函数与标准方式声明的函数区别在于,不需要使用 def 语句,也不需要一个名字来引用它。使用 lambda 语句可以直接得到一个函数对象,它的语法是: lambda [arg1[,arg2…]]: expression 参数可有可无,冒号后面是一个表达式,函数的作用就是返回这个表达式的值。在 def 语句下等同于: def func([arg1[,arg2…]]):return expression 可以看到 lambda 函数没有中间状态,也不适合构建过于复杂的函数,因为它的函数体只有一个表达式。所以应用 lambda 的场合更多是构建一些临时的、简单的和无需复用小函数。虽然原则上可以给 lambda 函数起别名,就像下面这样,以备后续引用。但如果你打算重复利用这个函数

js 关于apply和call的理解使用

时光毁灭记忆、已成空白 提交于 2019-12-05 17:15:39
关于call和apply,以前也思考良久,很多时候都以为记住了,但是,我太难了。今天我特地写下笔记,希望可以完全掌握这个东西,也希望可以帮助到任何想对学习这个东西的同学。 一.apply函数定义与理解,先从apply函数出发   在MDN上,apply的定义是:     “ apply() 方法调用一个具 有给定 this 值的函 数 ,以及作为一个数组(或 类似数组对象 )提供的参数。”   我的理解是:apply的前面有个含有this的对象,设为A,apply()的参数里,也含有一个含有this的对象设为B。则A.apply(B),表示A代码执行调用了B,B代码照常执行,执行后的结果作为apply的参数,然后apply把这个结果所指代表示的this替换掉A本身的this,接着执行A代码。   比如: 1 var aa = { 2 _name:111, 3 _age:222, 4 _f:function(){ 5 console.log(this) 6 console.log(this._name) 7 } 8 } 9 var cc = { 10 _name:0, 11 _age:0, 12 _f:function(){ 13 console.log(this) 14 console.log(this._name) 15 } 16 } 17 cc._f.apply(aa)/

Tabulating multiple response questions

孤街浪徒 提交于 2019-12-05 17:03:35
Imagine that I have a question for which there are four options, and a respondent can select zero or any combination of the four. The variables are named A , B , C , and D and the responses are stored in a data.frame as below. set.seed(1) dat = data.frame(A = sample(c(0, 1), 20, replace=TRUE), B = sample(c(0, 1), 20, replace=TRUE), C = sample(c(0, 1), 20, replace=TRUE), D = sample(c(0, 1), 20, replace=TRUE)) I can tabulate the combination of responses (for example, how many responded with A alone, or A + B , or C + D , and so on) by doing the following: data.frame(table(dat)) # A B C D Freq #

Outer Apply 和 Cross Apply

♀尐吖头ヾ 提交于 2019-12-05 16:32:49
1.場合 select...caseが複雑の時 2.運用方法 1 SELECT * 2 FROM stu 3 CROSS APPLY ( --like inner join 4 SELECT TOP 1 * FROM score WHERE stu.stuId=score.stuId ORDER BY score DESC 5 ) AS ff 6 /* 7 OUTER APPLY ( --like left join 8 SELECT TOP 1 * FROM score WHERE stu.stuId=score.stuId ORDER BY score DESC 9 ) AS ff 10 */ 虽然apply性能低,但是也有其用武之地,当需要按照顺序进行连接时,apply是最好的选择。 来源: https://www.cnblogs.com/wleaves/p/11933429.html

Improve efficiency for removing duplicate values per row and shift values in R

泄露秘密 提交于 2019-12-05 16:23:40
I have a huge dataset ( > 2.5 Million). A small subset looks like this (code reproducible) temp <- data.frame(list(col1 = c("424", "560", "557"), col2 = c("276", "427", "V46"), col3 = c("780", "V45", "584"), col4 = c("276", "V45", "995"), col5 = c("428", "799", "427"))) > temp col1 col2 col3 col4 col5 1 424 276 780 276 428 2 560 427 V45 V45 799 3 557 V46 584 995 427 I am trying to remove duplicates per row, and shifting values left, using this code library(plyr) temp <- apply(temp,1,function(x) unique(unlist(x))) temp <- ldply(temp, rbind) > temp 1 2 3 4 5 1 424 276 780 428 <NA> 2 560 427 V45

Apply lines() to columns of a data frame/matrix; each line with a different color

孤人 提交于 2019-12-05 15:54:05
I am trying to come up with a solution that doesn't involve using other packages such as ggplot. While plotting multiple lines is pretty straightforward, I haven't figured out a way to apply different values of an argument - e.g., different colors - to different lines. The code below (with the resulting plot) was my attempt, which obviously didn't do what I would like it to do. I also don't want to use a loop because I am trying to make my script as simple as possible. df = cbind(sort(rnorm(10)), sort(rnorm(10,-2)), sort(rlnorm(10))) plot(0, xlim = c(1,10), ylim=range(df), type="n") apply(df,

3D array -> apply -> 3D array

前提是你 提交于 2019-12-05 14:39:44
It seems apply will not re-assemble 3D arrays when operating on just one margin. Consider: arr <- array( runif(2*4*3), dim=c(2, 4, 3), dimnames=list(a=paste0("a", 1:2), b=paste0("b", 1:4), c=paste0("c", 1:3)) ) # , , c = c1 # # b # a b1 b2 b3 b4 # a1 0.7321399 0.8851802 0.2469866 0.9307044 # a2 0.5896138 0.6183046 0.7732842 0.6652637 # # , , c = c2 # b # a b1 b2 b3 b4 # a1 0.5894680 0.7839048 0.3854357 0.56555024 # a2 0.6158995 0.6530224 0.8401427 0.04044974 # # , , c = c3 # b # a b1 b2 b3 b4 # a1 0.3500653 0.7052743 0.42487635 0.5689287 # a2 0.4097346 0.4527939 0.07192528 0.8638655 Now, make