apply

Applying function to consecutive subvectors of equal size

不问归期 提交于 2019-12-01 05:18:36
问题 I am looking for a nice and fast way of applying some arbitrary function which operates on vectors, such as sum , consecutively to a subvector of consecutive K elements. Here is one simple example, which should illustrate very clearly what I want: v <- c(1, 2, 3, 4, 5, 6, 7, 8) v2 <- myapply(v, sum, group_size=3) # v2 should be equal to c(6, 15, 15) The function should try to process groups of group_size elements of a given vector and apply a function to each group (treating it as another

Rolling a function on a data frame

混江龙づ霸主 提交于 2019-12-01 05:13:49
I have the following data frame C . >>> C a b c 2011-01-01 0 0 NaN 2011-01-02 41 12 NaN 2011-01-03 82 24 NaN 2011-01-04 123 36 NaN 2011-01-05 164 48 NaN 2011-01-06 205 60 2 2011-01-07 246 72 4 2011-01-08 287 84 6 2011-01-09 328 96 8 2011-01-10 369 108 10 I would like to add a new column, d , where I apply a rolling function, on a fixed window (6 here), where I somehow, for each row (or date), fix the value c . One loop in this rolling function should be (pseudo): a b c d 2011-01-01 0 0 NaN a + b*2 (a,b from this row, '2' is from 'c' on 2011-01-06) 2011-01-02 41 12 NaN a + b*2 (a,b from this

Ignoring values or NAs in the sample function

梦想与她 提交于 2019-12-01 04:34:16
问题 I have a matrix in R that I would like to take a single random sample from each row. Some of my data is in NA, but when taking the random sample I do not want the NA to be an option for the sampling. How would I accomplish this? For example, a <- matrix (c(rep(5, 10), rep(10, 10), rep(NA, 5)), ncol=5, nrow=5) a [,1] [,2] [,3] [,4] [,5] [1,] 5 5 10 10 NA [2,] 5 5 10 10 NA [3,] 5 5 10 10 NA [4,] 5 5 10 10 NA [5,] 5 5 10 10 NA When I apply the sample function to this matrix to output another

Using a counter inside an apply structured loop in R

让人想犯罪 __ 提交于 2019-12-01 04:31:56
问题 I'm trying to plot from a rather complex array in R. I want to produce an image with 3 by 3 graphs, each with red and blue points on it. I've got a structure of apply loops which works, but I'd like to change the y maximum value by each row. I would normally do this using a counter, like i, in other languages. But the apply thing in R is completely baffling me! par(mfrow=c(3,3),pty="s") # a 3 by 3 graphic set.seed(1001) x <- 1:54 # with 1 to 54 along the x axis y <- array(rexp(20), dim=c(54,6

pandas, apply with args which are dataframe row entries

浪子不回头ぞ 提交于 2019-12-01 04:07:21
I have a pandas dataframe 'df' with two columns 'A' and 'B', I have a function with two arguments def myfunction(B, A): # do something here to get the result return result and I would like to apply it row-by-row to df using the 'apply' function df['C'] = df['B'].apply(myfunction, args=(df['A'],)) but I get the error ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). whats happening here, it seems it takes df['A'] as the whole series! not just the row entry from that series as required. I think you need: import pandas as pd df = pd

Why is apply not already bound to functions in Javascript?

人盡茶涼 提交于 2019-12-01 03:59:40
问题 Assume, for the sake of this question, that I want to be able to create a function in Javascript that appends all of the elements of one array to another array. One way to achieve this, if you have access to the destination array, is to say: var destination = [1,2,3]; var source = [4,5]; Array.prototype.push.apply(destination, source); console.log(destination); // [1,2,3,4,5] Now, since Array.prototype.push.apply is pretty ugly, I want to alias it to something nicer, like: var pushAll = Array

R: Apply function to matrix with elements of vector as argument

寵の児 提交于 2019-12-01 03:57:06
问题 Suppose I want to apply a function to each row of a matrix. One of the function's arguments takes a vector. I would like to apply the first element of the vector to the first row, the second element to the second row, etc. For example: set.seed(123) df<-matrix(runif(100), ncol=10) var2 <- c(1:10) MYFUNC <- function(x, Var=NA){ sum(x)/Var } I tried this: apply(df, 1, function(x) MYFUNC(x, Var=var2)) But that gives me a 10x10 matrix with the function applied to each row & Var combination,

Data Conversion Error while applying a function to each row in pandas Python

你。 提交于 2019-12-01 03:55:54
I have a data frame in pandas in python which resembles something like this - contest_login_count contest_participation_count ipn_ratio 0 1 1 0.000000 1 3 3 0.083333 2 3 3 0.000000 3 3 3 0.066667 4 5 13 0.102804 5 2 3 0.407407 6 1 3 0.000000 7 1 2 0.000000 8 53 91 0.264151 9 1 2 0.000000 Now I want to apply a function to each row of this dataframe The function is written as this - def findCluster(clusterModel,data): return clusterModel.predict(data) I apply this function to each row in this manner - df_fil.apply(lambda x : findCluster(cluster_all,x.reshape(1,-1)),axis=1) When I run this code,

How do I add random `NA`s into a data frame

江枫思渺然 提交于 2019-12-01 03:47:12
I created a data frame with random values n <- 50 df <- data.frame(id = seq (1:n), age = sample(c(20:90), n, rep = TRUE), sex = sample(c("m", "f"), n, rep = TRUE, prob = c(0.55, 0.45)) ) and would like to introduce a few NA values to simulate real world data. I am trying to use apply but cannot get there. The line apply(subset(df,select=-id), 2, function(x) {x[sample(c(1:n),floor(n/10))]}) will retrieve random values alright, but apply(subset(df,select=-id), 2, function(x) {x[sample(c(1:n),floor(n/10))]<-NA}) will not set them to NA . Have tried with and within , too. Brute force works: for (i

Pandas apply to dateframe produces '<built-in method values of …'

旧城冷巷雨未停 提交于 2019-12-01 03:36:45
I'm trying to build a GeoJSON object . My input is a csv with an address column, a lat column, and a lon column. I then created Shapely points out of the coordinates , buffer them out by a given radius, and get the dictionary of coordinates via the mapping option- so far, so good. Then, after referring to this question , I wrote the following function to get a Series of dictionaries: def make_geojson(row): return {'geometry':row['geom'], 'properties':{'address':row['address']}} and I applied it thusly: data['new_output'] = data.apply(make_geojson, axis=1) My resulting column is full of these: