apply

Handling NA values in apply and unique

随声附和 提交于 2019-12-01 03:23:14
I have a 114 row by 16 column data frame where the rows are individuals, and the columns are either their names or NA. For example, the first 3 rows looks like this: name name.1 name.2 name.3 name.4 name.5 name.6 name.7 name.8 name.9 name.10 name.11 name.12 name.13 name.14 name.15 1 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> Aanestad <NA> Aanestad <NA> Aanestad <NA> 2 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> Ackerman <NA> Ackerman <NA> Ackerman <NA> Ackerman <NA> 3 <NA> <NA> <NA> <NA> <NA> <NA> Alarcon <NA> Alarcon <NA> Alarcon <NA> Alarcon <NA> <NA> <NA> I want to generate a list (if

Why does sapply return a matrix that I need to transpose, and then the transposed matrix will not attach to a dataframe?

混江龙づ霸主 提交于 2019-12-01 01:07:21
问题 I would appreciate insight into why this happens and how I might do this more eloquently. When I use sapply, I would like it to return a 3x2 matrix, but it returns a 2x3 matrix. Why is this? And why is it difficult to attach this to another data frame? a <- data.frame(id=c('a','b','c'), var1 = c(1,2,3), var2 = c(3,2,1)) out <- sapply(a$id, function(x) out = a[x, c('var1', 'var2')]) #out is 3x2, but I would like it to be 2x3 #I then want to append t(out) (out as a 2x3 matrix) to b, a 1x3

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

两盒软妹~` 提交于 2019-12-01 00:42:35
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, 157122, 866381, 582868, 878) y <- c(356739, 324042, 904133, 959893, 433677, 110269, 576942, 2230, 267130,

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

…衆ロ難τιáo~ 提交于 2019-12-01 00:36:26
问题 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

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

谁说胖子不能爱 提交于 2019-12-01 00:17:09
问题 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

tidyr use separate_rows over multiple columns

别说谁变了你拦得住时间么 提交于 2019-11-30 23:52:56
问题 I have a data.frame where some cells contain strings of comma separate values: d <- data.frame(a=c(1:3), b=c("name1, name2, name3", "name4", "name5, name6"), c=c("name7","name8, name9", "name10" )) I want to separate those strings where each name is split into its own cell. This is easy with tidyr::separate_rows(d, b, sep=",") if it is done for one column a time. But I can't do this for both columns "b" and "c" at the same time, since it requires that the number of names in each string is the

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

為{幸葍}努か 提交于 2019-11-30 23:16:06
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? One Solution for your reference according to the sample you provide here. You didn't mention how to deal with ties. Here for ties, the first occurrence is fetched. data test; input var1 var2 var3; datalines; 10 40 60 15 10 5

rbind data frames based on a common pattern in data frame name

半城伤御伤魂 提交于 2019-11-30 22:43:35
Say I have multiple data frames which all have identical vector names and I'd like to cbind all which have a commmon pattern. So for these 3 data frames: df.1 <- data.frame(column1 = factor(sample(c("Male","Female"), 10, replace=TRUE)), speed=runif(10)) df.2 <- data.frame(column1 = factor(sample(c("Male","Female"), 10, replace=TRUE)), speed=runif(10)) df.3 <- data.frame(column1 = factor(sample(c("Male","Female"), 10, replace=TRUE)), speed = runif(10)) I would like to rbind everything with the common pattern "df.*" I have tried creating a list and then creating a data-frame from this using:

Applying a function to every combination of elements in a vector

拈花ヽ惹草 提交于 2019-11-30 22:18:53
I would like to apply a certain (custom) function to all combinations of an array. I think its best to explain with an example: Matrix 1 : A B C 1 2 3 Matrix 2 : A B C 4 5 6 I would like to do the following: obtain all the combinations of Matrix two and apply a function to each as follows: Matrix 3 : AB AC BC CB CA BA 4/2 4/3 5/3 6/2 6/1 5/1 Where the function applied to Matrix 3 is the corresponding element of Matrix 2 (represented by the first letter in each column of Matrix 3)/the corresponding element of Matrix 2 (represented by the second letter in each column in Matrix 3). Please let me

Javascript .apply() method equivalent in Java?

拜拜、爱过 提交于 2019-11-30 22:08:27
I want to create a class in Java from a classname and an variable number of arguments (i.e. an Object[] args with variable length). Is there any way to achieve that? Basically, the method would look like this in Javascript function createClass(classname, args) { protoObject = Object.create(window[classname].prototype); return window[classname].apply(protoObject,args) || protoObject; } // I want to be able to do this: var resultClass = createClass("someClass", someArrayOfArgs); A simpler function to only call a function would look like function callFunction(functionName, args) { return window