apply

Can I use apply() with constructor to pass arbitrary number of parameters

馋奶兔 提交于 2019-11-28 11:26:37
I've got a function wich can accept a varible number of parameter with a rest operator. I want create an object passing the argument collected with the rest operator directly to a constructor without create an object and call an initializing function and without passing the entire array but the parameters ah I do with apply() function. Is it possible ? Using apply doesn't work. public function myFunc(...arg) { // something link "new MyClass.apply(args)" return new MyClass(); } Unfortunately no. There is no way to make apply work for constructor. What is done generally is to prepare a number of

Count occurrences of items in Series in each row of a DataFrame

拟墨画扇 提交于 2019-11-28 11:10:02
I have a pandas.DataFrame that looks like this. COL1 COL2 COL3 C1 None None C1 C2 None C1 C1 None C1 C2 C3 For each row in this dataframe I would like to count the occurrences of each of C1, C2, C3 and append this information as columns to this dataframe. For instance, the first row has 1 C1, 0 C2 and 0 C3. The final data frame should look like this COL1 COL2 COL3 C1 C2 C3 C1 None None 1 0 0 C1 C2 None 1 1 0 C1 C1 None 2 0 0 C1 C2 C3 1 1 1 So, I have created a Series with C1, C2 and C3 as the values - one way top count this is to loop over the rows and columns of the DataFrame and then over

weighted means by group and column

*爱你&永不变心* 提交于 2019-11-28 10:31:04
I wish to obtain weighted means by group for each of several (actually about 60) columns. This question is very similar to: repeatedly applying ave for computing group means in a data frame just asked. I have come up with two ways to obtain the weighted means so far: use a separate sapply statement for each column place an sapply statement inside a for-loop However, I feel there must be a way to insert an apply statement inside the sapply statement or vice versa, thereby eliminating the for-loop . I have tried numerous permutations without success. I also looked at the sweep function. Here is

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

可紊 提交于 2019-11-28 10:12:09
问题 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

When we go for cross apply and when we go for inner join in SQL Server 2012

南笙酒味 提交于 2019-11-28 06:57:51
问题 I have small question about SQL Server. When do we use cross apply , and when do we use inner join ? Why use cross apply at all in SQL Server? I have emp, dept tables; based on those two tables, I write an inner join and cross apply query like this: ----using cross apply SELECT * FROM Department D CROSS APPLY (SELECT * FROM Employee E WHERE E.DepartmentID = D.DepartmentID) A ----using inner join SELECT * FROM Department D INNER JOIN Employee E ON D.DepartmentID = E.DepartmentID Both queries

R count times word appears in element of list

天涯浪子 提交于 2019-11-28 06:31:58
问题 I have a list comprised of words. > head(splitWords2) [[1]] [1] "Some" "additional" "information" "that" "we" "would" "need" "to" "replicate" "the" [11] "experiment" "is" "how" "much" "vinegar" "should" "be" "placed" "in" "each" [21] "identical" "container" "or" "what" "tool" "use" "measure" "mass" "of" "four" [31] "different" "samples" "and" "distilled" "water" "rinse" "after" "taking" "them" "out" [[2]] [1] "After" "reading" "the" "expirement" "I" "realized" "that" "additional" "information

Looping over combinations of regression model terms

浪尽此生 提交于 2019-11-28 06:13:58
问题 I'm running a regression in the form reg=lm(y ~ x1+x2+x3+z1,data=mydata) In the place of the last term, z1 , I want to loop through a set of different variables, z1 through z10 , running a regression for each with it as the last term. E.g. in second run I want to use reg=lm(y ~ x1+x2+x3+z2,data=mydata) in 3rd run: reg=lm(y ~ x1+x2+x3+z3,data=mydata) How can I automate this by looping through the list of z-variables? 回答1: With this dummy data: dat1 <- data.frame(y = rpois(100,5), x1 = runif

Faster alternative to iterrows

天大地大妈咪最大 提交于 2019-11-28 04:26:15
问题 I know that this topic has been addressed a thousand times. But I can't figure out a solution. I'm trying to count how often a list (each row of df1.list1) occurs in a column of list (df2.list2). All lists consist of unique values only. List1 includes about 300.000 rows and list2 30.000 rows. I've got a working code but its terribly slow (because I'm using iterrows). I also tried itertuples() but it gave me an error ("too many values to unpack (expected 2)"). I found a similar question online

Get the (t-1) data within groups

风格不统一 提交于 2019-11-28 04:08:59
问题 Apologies if this has been asked before, but I couldn't find any question which answers this exactly. I have a data like this: Project Date price A 30/3/2013 2082 B 19/3/2013 1567 B 22/2/2013 1642 C 12/4/2013 1575 C 5/6/2013 1582 I want to have a column with last-instance prices by group. For example, for row 2, the last instance price for same group will be 1642. The final data will look somewhat like this: Project Date price lastPrice A 30/3/2013 2082 0 B 19/3/2013 1567 1642 B 22/2/2013

What's the .apply jQuery function?

二次信任 提交于 2019-11-28 03:22:07
I see that in different plugins and codes, but I don't understand what does that function... In the jQuery api isn't referenced! Amber apply calls a function with a set of arguments. It's not part of jQuery, it's part of core Javascript. However, there is mention of it in the jQuery docs: http://docs.jquery.com/Types#Context.2C_Call_and_Apply Syntax: somefunction.apply(thisObj, [argsArray]) The above calls the function somefunction , setting this to thisObj within the function's scope, and passing in the arguments from argsArray as the arguments to the function. Essentially, apply will call a