apply

Speeding up Pandas apply function

霸气de小男生 提交于 2020-01-01 12:05:09
问题 For a relatively big Pandas DataFrame (a few 100k rows), I'd like to create a series that is a result of an apply function. The problem is that the function is not very fast and I was hoping that it can be sped up somehow. df = pd.DataFrame({ 'value-1': [1, 2, 3, 4, 5], 'value-2': [0.1, 0.2, 0.3, 0.4, 0.5], 'value-3': somenumbers..., 'value-4': more numbers..., 'choice-index': [1, 1, np.nan, 2, 1] }) def func(row): i = row['choice-index'] return np.nan if math.isnan(i) else row['value-%d' % i

Speeding up Pandas apply function

五迷三道 提交于 2020-01-01 12:04:05
问题 For a relatively big Pandas DataFrame (a few 100k rows), I'd like to create a series that is a result of an apply function. The problem is that the function is not very fast and I was hoping that it can be sped up somehow. df = pd.DataFrame({ 'value-1': [1, 2, 3, 4, 5], 'value-2': [0.1, 0.2, 0.3, 0.4, 0.5], 'value-3': somenumbers..., 'value-4': more numbers..., 'choice-index': [1, 1, np.nan, 2, 1] }) def func(row): i = row['choice-index'] return np.nan if math.isnan(i) else row['value-%d' % i

How does the [].push.apply work?

霸气de小男生 提交于 2020-01-01 06:11:09
问题 can someone please explain me how does this line of code work. [].push.apply(perms, permutation(arr.slice(0), start + 1, last)); This function generates an array of all permutations of an input array; var permutation = function(arr, start, last){ var length = arr.length; if(!start){ start = 0; } if(!last){ last = length - 1; } if( last === start){ return [arr]; } var temp; var perms = []; for(var i = start; i < length; i++){ swapIndex(arr, i, start); console.log(arr); [].push.apply(perms,

Apply a function over all combinations of arguments (output as list)

ぃ、小莉子 提交于 2020-01-01 05:47:08
问题 This solution is almost what I need, but do not worked to my case. Here is what I have tried: comb_apply <- function(f,...){ exp <- expand.grid(...,stringsAsFactors = FALSE) apply(exp,1,function(x) do.call(f,x)) } #--- Testing Code l1 <- list("val1","val2") l2 <- list(2,3) testFunc<-function(x,y){ list(x,y) } #--- Executing Test Code comb_apply(testFunc,l1,l2) comb_apply(paste,l1,l2) It works for paste example, but I get the message: Error in (function (x, y) : unused arguments (Var1 = "val1"

replace loops with apply family functions (or dplyr), using logical functions in R

大城市里の小女人 提交于 2019-12-31 04:02:26
问题 I have created this representative data frame that assigns condition categories using a for loop. df <- data.frame(Date=c("08/29/2011", "08/29/2011", "08/30/2011", "08/30/2011", "08/30/2011", "08/29/2012", "08/29/2012", "01/15/2012", "08/29/2012"), Time=c("09:45", "10:00", "13:00", "13:30", "10:14", "9:09", "11:23", "17:06", "12:20"), Diff = c(0.2,4.3,6.5,15.0, 16.5, 31, 30.2, 21.9, 1.9)) df1<- df %>% mutate(Accuracy=ifelse(Diff<=3, "Excellent", "TBD")) for(i in 1:nrow(df1)){ if(df1$Diff[i]>3

Table by row with R

拈花ヽ惹草 提交于 2019-12-31 03:02:30
问题 I would like to tabulate by row within a data frame. I can obtain adequate results using table within apply in the following example: df.1 <- read.table(text = ' state county city year1 year2 year3 year4 year5 1 2 4 0 0 0 1 2 2 5 3 10 20 10 NA 10 2 7 1 200 200 NA NA 200 3 1 1 NA NA NA NA NA ', na.strings = "NA", header=TRUE) tdf <- t(df.1) apply(tdf[4:nrow(tdf),1:nrow(df.1)], 2, function(x) {table(x, useNA = "ifany")}) Here are the results: [[1]] x 0 1 2 3 1 1 [[2]] x 10 20 <NA> 3 1 1 [[3]] x

Replacing matrix elements indexed by another matrix

流过昼夜 提交于 2019-12-31 03:02:14
问题 After several hours of searching, I am turning to your expertise. Beginner in R, I try to speed up my code. My goal is to replace the values in a matrix A . However, I want to replace values based on two vectors of another matrix B . B[, 1] is the name of row i of the matrix A . The second column, B[, 2] corresponds to the name of column of the matrix A . The first version of my code was to use the match function in a loop. for(k in 1:L){ i <- B[k,1] j <- B[k,2] d <- match(i,rownames(A)) e <-

Why does apply() return incorrect column types?

左心房为你撑大大i 提交于 2019-12-31 00:59:52
问题 I've recently started using R and the apply() function is tripping me up. I'd appreciate help with this: is.numeric(iris$Sepal.Length) # returns TRUE is.numeric(iris$Sepal.Width) # returns TRUE is.numeric(iris$Petal.Length) # returns TRUE is.numeric(iris$Petal.Width) # returns TRUE but, apply(iris, 2, FUN = is.numeric) returns Sepal.Length Sepal.Width Petal.Length Petal.Width Species FALSE FALSE FALSE FALSE FALSE What's going on? 回答1: They are all FALSE because apply() coerces iris to a

rolling computations in xts by month

我与影子孤独终老i 提交于 2019-12-30 10:50:54
问题 I am familiar with the zoo function rollapply which allows you to do rolling computations on zoo or xts objects and you can specify the rolling increment via the by parameter. I am specifically interested in applying a function every month but using all of the past daily data in the computation. For example say my data set looks like this: dte, val 1/01/2001, 10 1/02/2001, 11 ... 1/31/2001, 2 2/01/2001, 54 2/02/2001, 34 ... 2/30/2001, 29 I would like to select the end of each month and apply

Applying function (ks.test) between two data frames colum-wise in R

你说的曾经没有我的故事 提交于 2019-12-30 07:06:10
问题 My simple question is: How do you do a ks.test between two data frames column by column? Eg. We have two data frames: D1 <- data.frame(D$Ag, D$Al, D$As, D$Ba, D$Be, D$Ca, D$Cd, D$Co, D$Cu, D$Cr) D2 <- data.frame(S$Ag, S$Al, S$As, S$Ba, S$Be, S$Ca, S$Cd, S$Co, S$Cu, S$Cr) Note: this is just an example - real case would include much more columns and they contain concentrations of a certain element in a specific location. Now i would like to run a ks.test between the two data frames : ks.test(D