apply

R Apply() function on specific dataframe columns

霸气de小男生 提交于 2019-11-28 03:14:58
I want to use the apply function on a dataframe, but only apply the function to the last 5 columns. B<- by(wifi,(wifi$Room),FUN=function(y){apply(y, 2, A)}) This applies A to all the columns of y B<- by(wifi,(wifi$Room),FUN=function(y){apply(y[4:9], 2, A)}) This applies A only to columns 4-9 of y, but the total return of B strips off the first 3 columns... I still want those, I just don't want A applied to them. wifi[,1:3]+B also does not do what I expected/wanted. Using an example data.frame and example function (just +1 to all values) A <- function(x) x + 1 wifi <- data.frame(replicate(9,1:4

How can I call a javascript constructor using call or apply? [duplicate]

a 夏天 提交于 2019-11-28 02:54:45
This question already has an answer here: Use of .apply() with 'new' operator. Is this possible? 35 answers How could I generalise the function below to take N arguments? (Using call or apply?) Is there a programmatic way to apply arguments to 'new'? I don't want the constructor to be treated like a plain function. /** * This higher level function takes a constructor and arguments * and returns a function, which when called will return the * lazily constructed value. * * All the arguments, except the first are pased to the constructor. * * @param {Function} constructor */ function conthunktor

Results transposed with R apply [duplicate]

泪湿孤枕 提交于 2019-11-28 02:09:55
This question already has an answer here: Why apply() returns a transposed xts matrix? 1 answer Apologies, I just realised that this has already been answered here . This should be pretty basic but I do not really understand why it is happening. Can someone help? This is the simple code with the example 'data': applyDirichletPrior <- function (row_vector) { row_vector_added <- row_vector + min (row_vector) row_vector_result <- row_vector_added / sum(row_vector_added) } data <- matrix(c(1,2,3,4,5,6,7,8), nrow=2, ncol=4) applied <- apply(data, 1, function(x) applyDirichletPrior(x)) The output is

Python pandas calculate rolling stock beta using rolling apply to groupby object in vectorized fashion

こ雲淡風輕ζ 提交于 2019-11-28 02:05:03
问题 I have a large data frame, df, containing 4 columns: id period ret_1m mkt_ret_1m 131146 CAN00WG0 199609 -0.1538 0.047104 133530 CAN00WG0 199610 -0.0455 -0.014143 135913 CAN00WG0 199611 0.0000 0.040926 138334 CAN00WG0 199612 0.2952 0.008723 140794 CAN00WG0 199701 -0.0257 0.039916 143274 CAN00WG0 199702 -0.0038 -0.025442 145754 CAN00WG0 199703 -0.2992 -0.049279 148246 CAN00WG0 199704 -0.0919 -0.005948 150774 CAN00WG0 199705 0.0595 0.122322 153318 CAN00WG0 199706 -0.0337 0.045765 id period ret

Loop over rows of dataframe applying function with if-statement

孤者浪人 提交于 2019-11-27 20:04:49
问题 I'm new to R and I'm trying to sum 2 columns of a given dataframe, if both the elements to be summed satisfy a given condition. To make things clear, what I want to do is: > t.d<-as.data.frame(matrix(1:9,ncol=3)) > t.d V1 V2 V3 1 4 7 2 5 8 3 6 9 > t.d$V4<-rep(0,nrow(t.d)) > for (i in 1:nrow(t.d)){ + if (t.d$V1[i]>1 && t.d$V3[i]<9){ + t.d$V4[i]<-t.d$V1[i]+t.d$V3[i]} + } > t.d V1 V2 V3 V4 1 4 7 0 2 5 8 10 3 6 9 0 I need an efficient code, as my real dataframe has about 150000 rows and 200

How does one reduce a list of boolean values in Common Lisp?

别等时光非礼了梦想. 提交于 2019-11-27 17:51:06
问题 Given a list of values, I want to reduce the list to T if all the elements are not NIL, NIL if not. This gives me an error: (apply #'and (get-some-list)) As does this: (reduce #'and (get-some-list)) This is the best I've come up with: [11]> (defun my-and (x y) (and x y)) MY-AND [12]> (reduce #'my-and '(T T T T T)) T [13]> (reduce #'my-and '(T T T T NIL)) NIL Why is "#'and" invalid? Is there a more idiomatic way to do this in Common Lisp? 回答1: #'and is invalid because and is a macro, not a

How to output duplicated rows

…衆ロ難τιáo~ 提交于 2019-11-27 16:25:24
I have the following data: x1 x2 x3 x4 34 14 45 53 2 8 18 17 34 14 45 20 19 78 21 48 2 8 18 5 In rows 1 and 3; and 2 and 5 the values for columns X1;X2,X3 are equal. How can I output only those 4 rows, with equal numbers? The output should be in the following format: x1 x2 x3 x4 34 14 45 53 34 14 45 20 2 8 18 17 2 8 18 5 Please, ask me questions if something unclear. ADDITIONAL QUESTION: in the output x1 x2 x3 x4 34 14 45 53 34 14 45 20 2 8 18 17 2 8 18 5 find the sum of values in last column: x1 x2 x3 x4 34 14 45 73 2 8 18 22 You can do this with duplicated , which checks for rows being

Convert data frame to list

空扰寡人 提交于 2019-11-27 16:17:43
I am trying to go from a data frame to a list structure in R (and I know technically a data frame is a list). I have a data frame containing reference chemicals and their mechanisms different targets. For example, estrogen is an estrogen receptor agonist. What I would like is to transform the data frame to a list, because I am tired of typing out something like: refchem$chemical_id[refchem$target=="AR" & refchem$mechanism=="Agonist"] every time I need to access the list of specific reference chemicals. I would much rather access the chemicals by: refchem$AR$Agonist I am looking for a general

Apply a function to groups within a data.frame in R

谁说胖子不能爱 提交于 2019-11-27 13:52:56
问题 I am trying to get the cumulative sum of a variable (v) for groups ("a" and "b") within a dataframe. How can I get the result at the bottom -- whose rows are even numbered properly -- into column cs of my dataframe? > library(nlme) > g <- factor(c("a","b","a","b","a","b","a","b","a","b","a","b")) > v <- c(1,4,1,4,1,4,2,8,2,8,2,8) > cs <- rep(0,12) > d <- data.frame(g,v,cs) > d g v cs 1 a 1 0 2 b 4 0 3 a 1 0 4 b 4 0 5 a 1 0 6 b 4 0 7 a 2 0 8 b 8 0 9 a 2 0 10 b 8 0 11 a 2 0 12 b 8 0 > r=gapply

how to access global/outer scope variable from R apply function?

旧城冷巷雨未停 提交于 2019-11-27 13:30:56
I can't seem to make apply function access/modify a variable that is declared outside... what gives? x = data.frame(age=c(11,12,13), weight=c(100,105,110)) x testme <- function(df) { i <- 0 apply(df, 1, function(x) { age <- x[1] weight <- x[2] cat(sprintf("age=%d, weight=%d\n", age, weight)) i <- i+1 #this could not access the i variable in outer scope z <- z+1 #this could not access the global variable }) cat(sprintf("i=%d\n", i)) i } z <- 0 y <- testme(x) cat(sprintf("y=%d, z=%d\n", y, z)) Results: age=11, weight=100 age=12, weight=105 age=13, weight=110 i=0 y=0, z=0 Using the <<- operator