apply

weighted means by group and column

岁酱吖の 提交于 2019-12-17 19:26:07
问题 I wish to obtain weighted means by group for each of several (actually about 60) columns. This question is very similar to: repeatedly applying ave for computing group means in a data frame just asked. I have come up with two ways to obtain the weighted means so far: use a separate sapply statement for each column place an sapply statement inside a for-loop However, I feel there must be a way to insert an apply statement inside the sapply statement or vice versa, thereby eliminating the for

Row/column counter in 'apply' functions

大兔子大兔子 提交于 2019-12-17 17:44:14
问题 What if one wants to apply a functon i.e. to each row of a matrix, but also wants to use as an argument for this function the number of that row. As an example, suppose you wanted to get the n-th root of the numbers in each row of a matrix, where n is the row number. Is there another way (using apply only) than column-binding the row numbers to the initial matrix, like this? test <- data.frame(x=c(26,21,20),y=c(34,29,28)) t(apply(cbind(as.numeric(rownames(test)),test),1,function(x) x[2:3]^(1

R Apply() function on specific dataframe columns

帅比萌擦擦* 提交于 2019-12-17 17:33:09
问题 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. 回答1: Using an example data

How to output duplicated rows

廉价感情. 提交于 2019-12-17 14:58:15
问题 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

Convert data frame to list

泪湿孤枕 提交于 2019-12-17 14:56:11
问题 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

subset a column in data frame based on another data frame/list

筅森魡賤 提交于 2019-12-17 07:29:12
问题 I have the following table1 which is a data frame composed of 6 columns and 8083 rows. Below I am displaying the head of this table1 : |gene ID | prom_65| prom_66| amast_69| amast_70| p_value| |:--------------|---------:|---------:|---------:|---------:|---------:| |LdBPK_321470.1 | 24.7361| 25.2550| 31.2974| 45.4209| 0.2997430| |LdBPK_251900.1 | 107.3580| 112.9870| 77.4182| 86.3211| 0.0367792| |LdBPK_331430.1 | 72.0639| 86.1486| 68.5747| 77.8383| 0.2469355| |LdBPK_100640.1 | 43.8766| 53.4004

简明易懂的call apply

ぐ巨炮叔叔 提交于 2019-12-14 17:13:56
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 在iteye看到一篇对call解释得相当简明易懂,觉得得宣传一下 : http://uule.iteye.com/blog/1158829 一、方法的定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义:调用一个对象的一个方法,以另一个对象替换当前对象。 说明: call 方法可以用来代替另一个对象调用一个方法。call 方法可将一个函数的对象上下文从初始的上下文改变为由 thisObj 指定的新对象。 如果没有提供 thisObj 参数,那么 Global 对象被用作 thisObj。 apply方法: 语法:apply([thisObj[,argArray]]) 定义:应用某一对象的一个方法,用另一个对象替换当前对象。 说明: 如果 argArray 不是一个有效的数组或者不是 arguments 对象,那么将导致一个 TypeError。 如果没有提供 argArray 和 thisObj 任何一个参数,那么 Global 对象将被用作 thisObj, 并且无法被传递任何参数。 2、常用实例 a例 function add(a,b) { alert(a+b); }function sub(a,b) { alert(a-b); } add

Filter multiple columns based on same criteria in R

陌路散爱 提交于 2019-12-14 04:05:39
问题 I have a dataframe in which there are multiple columns (more than 30) that is saved in a list. I would like to apply the same criteria for all those columns without writing each code for each columns. I have example below to help understand my problem better A<-c("A","B","C","D","E","F","G","H","I") B<-c(0,0,0,1,2,3,0,0,0) C<-c(0,1,0,0,1,2,0,0,0) D<-c(0,0,0,0,1,1,0,1,0) E<-c(0,0,0,0,0,0,0,1,0) data<-data.frame(A,B,C,D,E) Let say I have the above df as an example and I have saved the list of

Working across sub-lists with apply() functions

﹥>﹥吖頭↗ 提交于 2019-12-14 03:22:48
问题 I am trying to the bootstrap the proportional occurrence of diet items for 7 individuals and calculate a sd() Lets say there are 9 prey items on the menu. Diet <- c("Beaver","Bird", "Bobcat","Coyote", "Deer", "Elk", "Porcupine", "Raccoon", "SmMamm") And that these prey items are eaten by 7 different individuals of the same species Inds <- c("P01", "P02", "P03", "P04", "P05", "P06", "P07") My goal is the bootstrap the proportional occurrence of each diet item for each individual. The loop

Pandas apply tuple unpack function on multiple columns

时间秒杀一切 提交于 2019-12-14 02:08:47
问题 Given a function that takes multiple arguments and returns multiple values as so: def tuple_unpack(value, another_value): ''' does some interesting stuff ... ''' return value, another_value Is there a way to apply such function to a pandas dataframe where for the 2 function arguments I can pass values from 2 columns, then unpack the output tuple on multiple colums as so: df[['value_col','another_value_col']] = df.apply(lambda df.col, df.col: tuple_unpack) 回答1: You can using concat , with