mean

R: How to calculate mean for each row with missing values using dplyr

若如初见. 提交于 2019-12-01 08:47:57
I want to calculate means over several columns for each row in my dataframe containing missing values, and place results in a new column called 'means.' Here's my dataframe: df <- data.frame(A=c(3,4,5),B=c(0,6,8),C=c(9,NA,1)) A B C 1 3 0 9 2 4 6 NA 3 5 8 1 The code below successfully accomplishes the task if columns have no missing values, such as columns A and B. library(dplyr) df %>% rowwise() %>% mutate(means=mean(A:B, na.rm=T)) A B C means <dbl> <dbl> <dbl> <dbl> 1 3 0 9 1.5 2 4 6 NA 5.0 3 5 8 1 6.5 However, if a column has missing values, such as C, then I get an error: > df %>% rowwise()

take the mean of rows in a matrix r

穿精又带淫゛_ 提交于 2019-12-01 08:09:04
I have the following matrix of depth and temperature data (855 rows, 2 col) and would like to take the mean of every 3 rows within each column. For example: [1,] -6.7 18.91 [2,] -5.4 18.91 [3,] -4.0 18.59 [4,] -6.7 20.37 [5,] -6.7 20.05 [6,] -2.7 20.21 [7,] -4.0 21.03 [8,] -5.4 20.70 [9,] -4.0 20.87 [10,] -2.7 21.37 [11,] -2.7 21.37 [12,] -2.7 21.37 mean(data[1:3,1]) mean(data[4:6,1]) for the entire matrix. How can I accomplish this without manually writing the code for the mean of every 3 rows? Any ideas or suggestions are greatly appreciated. Use rollapply function from zoo package. See

R: How to calculate mean for each row with missing values using dplyr

前提是你 提交于 2019-12-01 06:54:46
问题 I want to calculate means over several columns for each row in my dataframe containing missing values, and place results in a new column called 'means.' Here's my dataframe: df <- data.frame(A=c(3,4,5),B=c(0,6,8),C=c(9,NA,1)) A B C 1 3 0 9 2 4 6 NA 3 5 8 1 The code below successfully accomplishes the task if columns have no missing values, such as columns A and B. library(dplyr) df %>% rowwise() %>% mutate(means=mean(A:B, na.rm=T)) A B C means <dbl> <dbl> <dbl> <dbl> 1 3 0 9 1.5 2 4 6 NA 5.0

take the mean of rows in a matrix r

扶醉桌前 提交于 2019-12-01 06:25:14
问题 I have the following matrix of depth and temperature data (855 rows, 2 col) and would like to take the mean of every 3 rows within each column. For example: [1,] -6.7 18.91 [2,] -5.4 18.91 [3,] -4.0 18.59 [4,] -6.7 20.37 [5,] -6.7 20.05 [6,] -2.7 20.21 [7,] -4.0 21.03 [8,] -5.4 20.70 [9,] -4.0 20.87 [10,] -2.7 21.37 [11,] -2.7 21.37 [12,] -2.7 21.37 mean(data[1:3,1]) mean(data[4:6,1]) for the entire matrix. How can I accomplish this without manually writing the code for the mean of every 3

Why are `colMeans()` and `rowMeans()` functions faster than using the mean function with `lapply()`?

人走茶凉 提交于 2019-12-01 06:24:51
问题 What I want to ask is, algorithmically, what do the rowMeans() and colMeans() functions do to optimize speed? 回答1: In addition, consider what lapply() does. It sets up repeated calls to the function mean() . So as well as the overhead of actually computing a mean (which is done in fast C code), the lapply() version repeatedly incurs the overhead of the sanity checking code and method dispatch associated with mean() . rowMeans() and colMeans() incur only a single set of sanity checks as

Automatic minification with nodeJS and Gulp task runner

老子叫甜甜 提交于 2019-12-01 05:36:16
I need some advices to improve automatic minification with node and gulp. The main objective is generate dynamically the minified files(for JS and LESS) in development mode and change automatically normal files(js and less) to minified files in production mode. The scenario contains: NodeJS and ExpressJS for routing and environment configuration Jade as template engine Gulp (task runner) This is my setup: GULP I'm using nodemon in order to lauch server.js wich starts my node server. In this gulp file i have some tasks (['watch']) for watch the changes on JS and LESS files and minify them in

How to ignore values when using numpy.sum and numpy.mean in matrices

大兔子大兔子 提交于 2019-12-01 04:38:34
Is there a way to avoid using specific values when applying sum and mean in numpy? I'd like to avoid, for instance, the -999 value when calculating the result. In [14]: c = np.matrix([[4., 2.],[4., 1.]]) In [15]: d = np.matrix([[3., 2.],[4., -999.]]) In [16]: np.sum([c, d], axis=0) Out[16]: array([[ 7., 4.], [ 8., -998.]]) In [17]: np.mean([c, d], axis=0) Out[17]: array([[ 3.5, 2. ], [ 4. , -499. ]]) Use a masked array: >>> c = np.ma.array([[4., 2.], [4., 1.]]) >>> d = np.ma.masked_values([[3., 2.], [4., -999]], -999) >>> np.ma.array([c, d]).sum(axis=0) masked_array(data = [[7.0 4.0] [8.0 1.0]

“circular” mean in R

ⅰ亾dé卋堺 提交于 2019-12-01 03:31:28
Given a dataset of months, how do I calculate the "average" month, taking into account that months are circular? months = c(1,1,1,2,3,5,7,9,11,12,12,12) mean(months) ## [1] 6.333333 In this dummy example, the mean should be in January or December. I see that there are packages for circular statistics, but I'm not sure whether they suit my needs here. I think months <- c(1,1,1,2,3,5,7,9,11,12,12,12) library("CircStats") conv <- 2*pi/12 ## months -> radians Now convert from months to radians, compute the circular mean, and convert back to months. I'm subtracting 1 here assuming that January is

Automatic minification with nodeJS and Gulp task runner

旧时模样 提交于 2019-12-01 02:57:42
问题 I need some advices to improve automatic minification with node and gulp. The main objective is generate dynamically the minified files(for JS and LESS) in development mode and change automatically normal files(js and less) to minified files in production mode. The scenario contains: NodeJS and ExpressJS for routing and environment configuration Jade as template engine Gulp (task runner) This is my setup: GULP I'm using nodemon in order to lauch server.js wich starts my node server. In this

MATLAB实例:PCA降维

主宰稳场 提交于 2019-12-01 02:39:14
ISODATA聚类算法的matlab程序 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 参考: Kmeans及ISODATA算法的matlab实现 数据见: MATLAB实例:PCA降维 中的iris数据集,保存为:iris.data,最后一列是类标签。 demo_isodata.m clear clc data_load=dlmread('iris.data'); [~,dim]=size(data_load); x=data_load(:,1:dim-1); K=3; theta_N=1; theta_S=1; theta_c=4; L=1; I=5; ISODATA(x,K,theta_N,theta_S,theta_c,L,I) ISODATA.m function ISODATA(x,K,theta_N,theta_S,theta_c,L,I) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%input parameters%%%%%% % x : data % K : 预期的聚类中心数 % theta_N : 每一聚类中心中最少的样本数,少于此数就不作为一个独立的聚类 % theta_S :一个聚类中样本距离分布的标准差 % theta_c :