apply

Using apply with a different function argument for each element's evaluation

久未见 提交于 2019-12-02 15:05:22
问题 Let's say I have a matrix, mat. mat <- matrix(1:5, nrow = 10, ncol = 3, byrow = TRUE) And I have some sort of function that I want to apply, in this case by column. getRMSE <- function(true, est) { sqrt(mean((true - est)^2)) } (This function is just the most recent example, but I've had this exact same conundrum at least 5 more times.) If you want to apply a function over a matrix, you use apply. But what if you want to apply a function over a matrix with different values for 'x' in the apply

multiply multiple column and find sum of each column for multiple values

被刻印的时光 ゝ 提交于 2019-12-02 13:45:17
I'm trying to multiply column and get its names. I have a data frame: v1 v2 v3 v4 v5 0 1 1 1 1 0 1 1 0 1 1 0 1 1 0 I'm trying to multiplying each column with other, like: v1v2 v1v3 v1v4 v1v5 and v2v3 v2v4 v2v5 etc, and v1v2v3 v1v2v4 v1v2v5 v2v3v4 v2v3v5 4 combination and 5 combination...if there is n column then n combination. I'm try to use following code in while loop, but it is not working: i<-1 while(i<=ncol(data) { results<-data.frame() v<-i results<- t(apply(data,1,function(x) combn(x,v,prod))) comb <- combn(colnames(data),v) colnames(results) <- apply(comb,v,function(x) paste(x[1],x[2]

Sum Every N Values in Matrix

回眸只為那壹抹淺笑 提交于 2019-12-02 12:14:38
问题 So I have taken a look at this question posted before which was used for summing every 2 values in each row in a matrix. Here is the link: sum specific columns among rows. I also took a look at another question here: R Sum every k columns in matrix which is more similiar to mine. I could not get the solution in this case to work. Here is the code that I am working with... y <- matrix(1:27, nrow = 3) y m1 <- as.matrix(y) n <- 3 dim(m1) <- c(nrow(m1)/n, ncol(m1), n) res <- matrix(rowSums(apply

New column using apply function on other columns in dataframe

╄→尐↘猪︶ㄣ 提交于 2019-12-02 11:24:33
问题 I have a dataframe where three of the columns are coordinates of data ('H_x', 'H_y' and 'H_z'). I want to calculate radius-vector of the data and add it as a new column in my dataframe. But I have some kind of problem with pandas apply function. My code is: def radvec(x, y, z): rv=np.sqrt(x**2+y**2+z**2) return rv halo_field['rh_field']=halo_field.apply(lambda row: radvec(row['H_x'], row['H_y'], row['H_z']), axis=1) The error I'm getting is: group_sh.py:78: SettingWithCopyWarning: A value is

what is apply method in Scala, especially used in type definition

风格不统一 提交于 2019-12-02 10:57:46
问题 I know that apply method is syntactic sugar when used in companion object. However, what is apply method for when it is used in type definition just like below? type Applyn = { def apply[A](f: A=>A, n: Int, x: A): A } Is there a deference between this sentence? As I guess, this sentence is used for assigning generic function value to Applyn. For example, by using above sentence, we can make (Int=>Int, Int, Int)=>Int (String=>String, Int, String)=>String etc., into only a single type Applyn .

Apply/Call method in Javascript: What is the first arguments “this”?

寵の児 提交于 2019-12-02 10:05:03
问题 I am confused about using apply or call method correctly. I know that apply is passing an array to the function and call is passing strings to a function. For example the code below, what does "this"really have to do with the code? if it has nothing to do with this code, then can anyone give me an example when "this" is implementing appropriately? function myFunction(a, b) { return a * b; } myArray = [10,2]; myFunction.apply(this, myArray); 回答1: It's the context for the function. If you have

R: How to create multiple maps (rworldmap) using apply?

回眸只為那壹抹淺笑 提交于 2019-12-02 09:58:29
I want to create multiple maps (similar to this example) using the apply family. Here a small sample of my code (~200 rows x 150 cols). (UN and ISO3 are codes for rworldmap): df <- structure(list(BLUE.fruits = c(12803543, 3745797, 19947613, 0, 130, 4), BLUE.nuts = c(21563867, 533665, 171984, 0, 0, 0), BLUE.veggies = c(92690, 188940, 34910, 0, 0, 577), GREEN.fruits = c(3389314, 15773576, 8942278, 0, 814, 87538 ), GREEN.nuts = c(6399474, 1640804, 464688, 0, 0, 0), GREEN.veggies = c(15508, 174504, 149581, 0, 0, 6190), UN = structure(c(4L, 5L, 1L, 6L, 2L, 3L), .Label = c("12", "24", "28", "4", "8"

Selecting rows from a data frame from combinations of lists [duplicate]

北战南征 提交于 2019-12-02 09:30:51
This question already has an answer here: Removing one table from another in R [closed] 3 answers I have a dataframe, dat: dat<-data.frame(col1=rep(1:4,3), col2=rep(letters[24:26],4), col3=letters[1:12]) I want to filter dat on two different columns using ONLY the combinations given by the rows in the data frame filter : filter<-data.frame(col1=1:3,col2=NA) lists<-list(list("x","y"),list("y","z"),list("x","z")) filter$col2<-lists So for example, rows containing (1,x) and (1,y), would be selected, but not (1,z),(2,x), or (3,y). I know how I would do it using a for loop: #create a frame to drop

Append onclick method using for loop

泄露秘密 提交于 2019-12-02 09:27:11
I'm appending onclick events to elements that I'm creating dynamically. I'm using the code below, this is the important part only. Test.prototype.Show= function (contents) { for (i = 0; i <= contents.length - 1; i++) { var menulink = document.createElement('a'); menulink.href = "javascript:;"; menulink.onclick = function () { return that.ClickContent.apply(that, [contents[i]]); }; } } First it says that it's undefined. Then I changed and added: var content = content[i]; menulink.onclick = function () { return that.ClickContent.apply(that, [content]); }; What is happening now is that it always

Aggregate sum obs with different ID's in the same data frame

南楼画角 提交于 2019-12-02 09:12:35
My goal is to make another column by summing the observation from the present day and all previous observations from the same ID by using the date (the data set is sorted in date and chr nr(ID). I will need the aggregation to start over when a new "id" is presented. there might be som NA's, they should be considered as null "Doseringer_pr_kg_dyr" is the observation. CHR_NR DATO_AFSLUT Doseringer_pr_kg_dyr brugstid 10358 2018-08-06 29416.67 31 10358 2018-09-06 104682.27 36 10358 2018-10-12 10333.33 26 10358 2018-11-07 10090.91 27 10358 2018-12-04 8000.00 NA 13168 2012-01-23 12042.25 2 13168