mean

calculate the mean for each column of a matrix in R

有些话、适合烂在心里 提交于 2019-11-26 08:45:51
I am working on R in R studio. I need to calculate the mean for each column of a data frame. cluster1 // 5 by 4 data frame mean(cluster1) // I got : Warning message: In mean.default(cluster1) : argument is not numeric or logical: returning NA But I can use mean(cluster1[[1]]) to get the mean of the first column. How to get means for all columns ? Any help would be appreciated. You can use colMeans : ### Sample data set.seed(1) m <- data.frame(matrix(sample(100, 20, replace = TRUE), ncol = 4)) ### Your error mean(m) # [1] NA # Warning message: # In mean.default(m) : argument is not numeric or

Calculate mean across dimension in a 2D array

蹲街弑〆低调 提交于 2019-11-26 07:28:37
问题 I have an array a like this: a = [[40, 10], [50, 11]] I need to calculate the mean for each dimension separately, the result should be this: [45, 10.5] 45 being the mean of a[*][0] and 10.5 the mean of a[*][1] . What is the most elegant way of solving this without using a loop? 回答1: a.mean() takes an axis argument: In [1]: import numpy as np In [2]: a = np.array([[40, 10], [50, 11]]) In [3]: a.mean(axis=1) # to take the mean of each row Out[3]: array([ 25. , 30.5]) In [4]: a.mean(axis=0) # to

how to calculate mean/median per group in a dataframe in r [duplicate]

廉价感情. 提交于 2019-11-26 05:35:35
问题 This question already has an answer here: Mean per group in a data.frame [duplicate] 8 answers I have a dataframe recording how much money a costomer spend in detail like the following: custid, value 1, 1 1, 3 1, 2 1, 5 1, 4 1, 1 2, 1 2, 10 3, 1 3, 2 3, 5 How to calcuate the charicteristics using mean,max,median,std, etc like the following? Use some apply function? And how? custid, mean, max,min,median,std 1, .... 2,.... 3,.... 回答1: To add to the alternatives, here's summaryBy from the "doBy"

Reading multiple files and calculating mean based on user input

只愿长相守 提交于 2019-11-26 04:51:14
问题 I am trying to write a function in R which takes 3 inputs: Directory pollutant id I have a directory on my computer full of CSV\'s files i.e. over 300. What this function would do is shown in the below prototype: pollutantmean <- function(directory, pollutant, id = 1:332) { ## \'directory\' is a character vector of length 1 indicating ## the location of the CSV files ## \'pollutant\' is a character vector of length 1 indicating ## the name of the pollutant for which we will calculate the ##

Python Pandas : group by in group by and average?

蹲街弑〆低调 提交于 2019-11-26 04:22:18
I have a dataframe like this: cluster org time 1 a 8 1 a 6 2 h 34 1 c 23 2 d 74 3 w 6 I would like to calculate the average of time per org per cluster. Expected result: cluster mean(time) 1 15 ((8+6)/2+23)/2 2 54 (74+34)/2 3 6 I do not know how to do it in Pandas, can anybody help? If you want to first take mean on ['cluster', 'org'] combination and then again take mean on cluster groups In [59]: (df.groupby(['cluster', 'org'], as_index=False).mean() .groupby('cluster')['time'].mean()) Out[59]: cluster 1 15 2 54 3 6 Name: time, dtype: int64 If you wan't mean values by cluster only, then you

Calculating arithmetic mean (one type of average) in Python

元气小坏坏 提交于 2019-11-26 03:33:03
问题 Is there a built-in or standard library method in Python to calculate the arithmetic mean (one type of average) of a list of numbers? 回答1: I am not aware of anything in the standard library. However, you could use something like: def mean(numbers): return float(sum(numbers)) / max(len(numbers), 1) >>> mean([1,2,3,4]) 2.5 >>> mean([]) 0.0 In numpy, there's numpy.mean(). 回答2: NumPy has a numpy.mean which is an arithmetic mean. Usage is as simple as this: >>> import numpy >>> a = [1, 2, 4] >>>

calculate the mean for each column of a matrix in R

时间秒杀一切 提交于 2019-11-26 02:01:12
问题 I am working on R in R studio. I need to calculate the mean for each column of a data frame. cluster1 // 5 by 4 data frame mean(cluster1) // I got : Warning message: In mean.default(cluster1) : argument is not numeric or logical: returning NA But I can use mean(cluster1[[1]]) to get the mean of the first column. How to get means for all columns ? Any help would be appreciated. 回答1: You can use colMeans : ### Sample data set.seed(1) m <- data.frame(matrix(sample(100, 20, replace = TRUE), ncol

Python Pandas : group by in group by and average?

流过昼夜 提交于 2019-11-25 23:46:44
问题 I have a dataframe like this: cluster org time 1 a 8 1 a 6 2 h 34 1 c 23 2 d 74 3 w 6 I would like to calculate the average of time per org per cluster. Expected result: cluster mean(time) 1 15 ((8+6)/2+23)/2 2 54 (74+34)/2 3 6 I do not know how to do it in Pandas, can anybody help? 回答1: If you want to first take mean on ['cluster', 'org'] combination and then again take mean on cluster groups In [59]: (df.groupby(['cluster', 'org'], as_index=False).mean() .groupby('cluster')['time'].mean())

Calculate group mean (or other summary stats) and assign to original data

感情迁移 提交于 2019-11-25 21:52:37
问题 I want to calculate mean (or any other summary statistics of length one, e.g. min , max , length , sum ) of a numeric variable (\"value\") within each level of a grouping variable (\"group\"). The summary statistic should be assigned to a new variable which has the same length as the original data . That is, each row of the original data should have a value corresponding to the current group value - the data set should not be collapsed to one row per group. For example, consider group mean :