apply

apply() is giving NA values for every column

浪尽此生 提交于 2019-11-30 20:57:53
I've been having this strange problem with apply lately. Consider the following example: set.seed(42) df <- data.frame(cars, foo = sample(LETTERS[1:5], size = nrow(cars), replace = TRUE)) head(df) speed dist foo 1 4 2 E 2 4 10 E 3 7 4 B 4 7 22 E 5 8 16 D 6 9 10 C I want to use apply to apply a function fun (say, mean ) on each column of that data.frame . If the data.frame is containing only numeric values, I do not have any problem: apply(cars, 2, mean) speed dist 15.40 42.98 But when trying with my data.frame containing numeric and character data, it seem to fail: apply(df, 2, mean) speed

Computing pairwise Hamming distance between all rows of two integer matrices/data frames

拈花ヽ惹草 提交于 2019-11-30 19:31:40
问题 I have two data frames, df1 with reference data and df2 with new data. For each row in df2 , I need to find the best (and the second best) matching row to df1 in terms of hamming distance. I used e1071 package to compute hamming distance. Hamming distance between two vectors x and y can be computed as for example: x <- c(356739, 324074, 904133, 1025460, 433677, 110525, 576942, 526518, 299386, 92497, 977385, 27563, 429551, 307757, 267970, 181157, 3796, 679012, 711274, 24197, 610187, 402471,

Add the index of the column with the maximum value as a new column

柔情痞子 提交于 2019-11-30 18:11:23
问题 My question is simple. When data is as below, var1 var2 var3 10 40 60 15 10 5 I want to add a new column MaxValueVar that returns index of a column that has maximum value among var1 , var2 and var3 . That is, I want to make a table as below. var1 var2 var3 MaxValueVar 10 40 60 3 15 10 5 1 In R I would use: apply(vector, 1, which.max) How can I accomplish this using SAS? 回答1: One Solution for your reference according to the sample you provide here. You didn't mention how to deal with ties.

apply different functions to different elements of a vector in R

你说的曾经没有我的故事 提交于 2019-11-30 18:03:45
问题 apply is easy, but this is a nutshell for me to crack: In multi-parametric regression, optimisers are used to find a best fit of a parametric function to say x1,x2 Data. Often, and function specific, optimisers can be faster if they try to optimise transformed parameters (e.g. with R optimisers such as DEoptim, nls.lm) From experience I know, that different transformations for different parameters from one parametric function is even better. I wish to apply different functions in x.trans (c.f

Perform pairwise comparison of matrix

北战南征 提交于 2019-11-30 17:04:44
I have a matrix of n variables and I want to make an new matrix that is a pairwise difference of each vector, but not of itself. Here is an example of the data. Transportation.services Recreational.goods.and.vehicles Recreation.services Other.services 2.958003 -0.25983789 5.526694 2.8912009 2.857370 -0.03425164 5.312857 2.9698044 2.352275 0.30536569 4.596742 2.9190123 2.093233 0.65920773 4.192716 3.2567390 1.991406 0.92246531 3.963058 3.6298314 2.065791 1.06120930 3.692287 3.4422340 I tried running a for loop below, but I'm aware that R is very slow with loops. Difference.Matrix<- function

What's the difference between a regular push and an Array.prototype.push.apply

眉间皱痕 提交于 2019-11-30 13:54:43
I don't quite understand the difference between the following two lines of code. In my code, the line with "apply" works the way I want it to, and the line with just regular push doesn't. So what is really going on when both of these are executed: //this one does not work the way i want it to $scope.items.push(result.data.stuff) //this one works! Array.prototype.push.apply($scope.items, result.data.stuff); Edit: sorry for confusion, I fixed it so that it has the "push" method in there New 1. That pushes the array onto items. $scope.items = [1, 2]; result.data.stuff = [3, 4]; $scope.items.push

R: rownames, colnames, dimnames and names in apply

拈花ヽ惹草 提交于 2019-11-30 13:49:33
I would like to use apply to run across the rows of a matrix, and I would like to use the rowname of the current row in my function. It seems you can't use rownames , colnames , dimnames or names directly inside the function. I am aware that I can probably create a workaround based on information in this question . But my question is how does apply handle row and column names of the array in it's first argument, and the assignment of names to objects created inside the function called by apply ? It seems a bit inconsistent, as I hope to show by the following example. Is there a reason why it

How can I construct an object using an array of values for parameters, rather than listing them out, in JavaScript?

扶醉桌前 提交于 2019-11-30 13:04:53
问题 Is this possible? I am creating a single base factory function to drive factories of different types (but have some similarities) and I want to be able to pass arguments as an array to the base factory which then possibly creates an instance of a new object populating the arguments of the constructor of the relevant class via an array. In JavaScript it's possible to use an array to call a function with multiple arguments by using the apply method: namespace.myFunc = function(arg1, arg2) { /

Apply function to each cell in DataFrame

笑着哭i 提交于 2019-11-30 12:23:30
问题 I have a dataframe that may look like this: A B C foo bar foo bar bar foo foo bar I want to look through every element of each row (or every element of each column) and apply the following function to get the subsequent DF: def foo_bar(x): return x.replace('foo', 'wow') A B C wow bar wow bar bar wow wow bar Is there a simple one-liner that can apply a function to each cell? This is a simplistic example so there may be an easier way to execute this specific example other than applying a

How does function.apply.bind work in the following code?

。_饼干妹妹 提交于 2019-11-30 12:15:12
问题 So I get that an array of [200,599] is returned from the promise and the callback function inside spread is being passed into Function.apply.bind, but now I'm lost. How is the array of [200,599] split into x and y? How exactly does the apply.bind work? function getY(x) { return new Promise( function(resolve,reject){ setTimeout( function(){ resolve( (3 * x) - 1 ); }, 100 ); } ); } function foo(bar,baz) { var x = bar * baz; // return both promises return [ Promise.resolve( x ), getY( x ) ]; }