apply

calculate row sum and product in data.frame

左心房为你撑大大i 提交于 2020-01-20 05:45:11
问题 I would like to append a columns to my data.frame in R that contain row sums and products Consider following data frame x y z 1 2 3 2 3 4 5 1 2 I want to get the following x y z sum prod 1 2 3 6 6 2 3 4 9 24 5 1 2 8 10 I have tried sum = apply(ages,1,add) but it gives me a row vector. Can some one please show me an efficient command to sum and product and append them to original data frame as shown above? 回答1: Try transform(df, sum=rowSums(df), prod=x*y*z) # x y z sum prod #1 1 2 3 6 6 #2 2 3

calculate row sum and product in data.frame

我与影子孤独终老i 提交于 2020-01-20 05:45:05
问题 I would like to append a columns to my data.frame in R that contain row sums and products Consider following data frame x y z 1 2 3 2 3 4 5 1 2 I want to get the following x y z sum prod 1 2 3 6 6 2 3 4 9 24 5 1 2 8 10 I have tried sum = apply(ages,1,add) but it gives me a row vector. Can some one please show me an efficient command to sum and product and append them to original data frame as shown above? 回答1: Try transform(df, sum=rowSums(df), prod=x*y*z) # x y z sum prod #1 1 2 3 6 6 #2 2 3

Vectorize() vs apply()

限于喜欢 提交于 2020-01-20 03:16:07
问题 The Vectorize() and the apply() functions in R can often be used to accomplish the same goal. I usually prefer vectorizing a function for readability reasons, because the main calling function is related to the task at hand while sapply is not. It is also useful to Vectorize() when I am going to be using that vectorized function multiple times in my R code. For instance: a <- 100 b <- 200 c <- 300 varnames <- c('a', 'b', 'c') getv <- Vectorize(get) getv(varnames) vs sapply(varnames, get)

$apply in setTimeout

谁都会走 提交于 2020-01-17 07:32:05
问题 I konw that $apply used to connect the Javascript context and the AngularJS context. A simple example is below: template: <div>{{someVal}}</div> javascript in controller: setTimeout(function() { scope.$apply(function(){scope.someVal = 123}); }, 1000); We need use $apply in above situation. First Qustion: If I modify javascript above to: setTimeout(function() { scope.someVal = 123; }, 1000); scope.$watch('someVal', function(val) { console.info(someVal); }); No console about someVal modified to

R: apply a function to a list of dataframes and save to workspace

∥☆過路亽.° 提交于 2020-01-17 07:12:49
问题 I have a lot of tibbles similar to this one: dftest_tw <- structure(list(text = c("RT @BitMEXdotcom: A new high: US$500M turnover in the last 24 hours, over 80% of it on $XBTUSD. Congrats to the team and thank you to our u…", "RT @Crowd_indicator: Thank you for this nice video, @Nicholas_Merten", "RT @Crowd_indicator: Review of #Cindicator by DataDash: t.co/D0da3u5y3V" ), Tweet.id = c("896858423521837057", "896858275689398272", "896858135314538497" ), created.date = structure(c(17391, 17391,

前端基础回顾之手写题

爱⌒轻易说出口 提交于 2020-01-17 05:07:47
前言 本文还是依然针对前端重点基础知识点进行整体回顾系列的一篇,目标是帮助自己理解避免死记硬背。 下面针对new、Object.create、call、apply、new、bind 等基础API,从用法到原理实现过一遍,期望看完之后大家实现时不是死记硬背而是根据理解记忆推导。 基础准备 在探究上述内容原理之前,可以将上述API分为两类。 一类是new、Object.create这两者,涉及实例化对象的。 其对应的基础内容部分和 上篇前端 面试 基础回顾之深入JS继承的基础部分相同。就是原型链和构造函数,这里不再赘述。 剩下的就是关于this指向的修改。 这里我们可以看下 MDN中对this的描述 。 this由调用时环境确定,简单总结如下: 显式指定: new 实例化 this指向新构建的对象(new 显式返回一个对象,则this指向该返回对象,否则指向该对象实例) // 例如 var bar = new foo() bind、call、apply ,指向绑定对象 var bar = foo.call( obj2 ) 隐式指定: 函数作为对象属性调用,即如object.func()形式,指向该对象。 //指向obj1 obj1.foo() 无指定 即不属于以上情况,为默认绑定。在strict mode下,就是undefined,否则是global对象。 var fun1 =

How do I apply unknown arguments to a function in Python 3.x?

梦想与她 提交于 2020-01-16 19:39:39
问题 I'm developing my own GUI for my engine, and I've created an "object" called EwConsole (A simple "drop-down" console to input commands). In order to do that, I simply store an ID for the function to be "triggered with enter/return" as the key to the function-object itself in a dict. My problem is with the arguments, and Python 3.x. Here's my method for creating a new "console command": def create_command(self, command_id, command_function): self["commands"][command_id] = command_function I've

how to run chisq.test in loops using apply

好久不见. 提交于 2020-01-16 19:14:19
问题 I am a newbie of R. Due to the need of my project, I need to do Chisq test for hundred thousand entries. I learned by myself for a few days and write some code for runing chisq.test in loops. codes: the.data = read.table ("test_chisq_allelefrq.txt", header=T, sep="\t",row.names=1) p=c() ID=c() for (i in 1:nrow(the.data)) { data.row = the.data [i,] data.matrix = matrix ( c(data.row$cohort_1_AA, data.row$cohort_1_AB, data.row$cohort_1_BB, data.row$cohort_2_AA, data.row$cohort_2_AB, data.row

R - Create a ordered list from a matrix

丶灬走出姿态 提交于 2020-01-16 18:39:33
问题 Hi there! I have this 24x24 matrix with a lot of numbers from 1-24. Where I want to turn every cell with the value of X (eg. 3) into the row.name of the X'th row (eg. name3) What I have row.names V1 V2 V3 name1 1 3 10 name2 3 20 1 name3 5 13 2 ... .. .. .. name24 19 3 4 What I want to do now, is turn all these numbers into a character string equvivalent to the i'th row name so a 1 should be converted to name1 and so forth. What I need row.names V1 V2 V3 name1 name1 name3 name10 name2 name3

apply a function to a groupby function

前提是你 提交于 2020-01-16 04:21:05
问题 I want to count how many consistent increase, and the difference between the first element and the last element, on a groupby. But I can't apply the function on the groupby. After groupby, is it a list? And also what's the difference between "apply" and "agg"? Sorry, I just touched the python for a few days. def promotion(ls): pro =0 if len(ls)>1: for j in range(1,len(ls)): if ls[j]>ls[j-1]: pro + = 1 return pro def growth(ls): head= ls[0] tail= ls[len(ls)-1] gro= tail-head return gro