logo
down
shadow

Average of subgroup of 2nd column, grouped by 1st column


Average of subgroup of 2nd column, grouped by 1st column

Content Index :

Average of subgroup of 2nd column, grouped by 1st column
Tag : matlab , By : user143038
Date : December 01 2020, 05:00 PM

To fix this issue I hadn't used accumarray before, so due to the comment by @Dan I decided to give it a try.
At first I tried a naive version and used histc to count occurrences to get the desired mean values... (Note that accumarray will sort the output the same order as unique, so mean will be calculated correctly)
%// Naive version
ua = unique(A(:,1)); %// use as histc bins (or sorted "group" values)
result = accumarray(A(:,1), A(:,2)) ./ histc(A(:,1), uA);
%// one-liner
result = accumarray(A(:,1), A(:,2), [], @mean);

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Average of rows where column = A within distinct rows on another column grouped by a third column


Tag : sql , By : Denis Chaykovskiy
Date : March 29 2020, 07:55 AM
this one helps. The answer is: It depends.
In my testing, my solution is the slowest of the bunch, regardless of what test data I use. With real life data, it's about half the speed of the fastest solution.

Google Sheets: how to find the average of values in one column grouped by another column's values?


Tag : google-sheets , By : Olexiy P.
Date : March 29 2020, 07:55 AM
it fixes the issue Let's say I have a Google Sheet with the following data: , By a Pivot Table
Range: Sheet1!A1:B7

Rows: 
   Group by: Column1

Columns: 
   Group by: Column2

Calculated Field: 
   Forumula: =AVERAGE=(Column2)
   Summarise by: Custom
            1   2   3   4   Grand Total
A           1   2   3   4   2.5
B           1   2           1.5
Grand Total 1   2   3   4   2.166666667

Average on column grouped by another column


Tag : mysql , By : cheese_doodle
Date : March 29 2020, 07:55 AM
Hope this helps I have a MySQL server with a table myTable which has the following columns: , I finally got the answer. I order to get the daily average I can use:
 SELECT DATE(FROM_UNIXTIME(timestamp)) AS Date, 
        AVG(temperature) AS AvgTemp
 FROM   `myTable`
 GROUP BY DATE(FROM_UNIXTIME(timestamp))
 SELECT DATE(FROM_UNIXTIME(timestamp)) AS Date,
        HOUR(FROM_UNIXTIME(timestamp)) AS Hour, 
        AVG(temperature) as AvgTemp
 FROM   `myTable`
 GROUP BY DATE(FROM_UNIXTIME(timestamp)),
          HOUR(FROM_UNIXTIME(timestamp))

R: Transfer one column into two column header and average the correct grouped data


Tag : r , By : Oli
Date : March 29 2020, 07:55 AM
should help you out Below is the input:
df$averages = ave(df[,"X5"], df[c("X1", "X2", "X3", "X4")], FUN = mean)
aggregate(averages~., df[c("averages", "X1", "X2", "X3")], range)
#   X1  X2  X3 averages.1 averages.2
#1 aaa bbb ccc        450        888
#2 qqq rrr ttt        400        456

Add column with average value grouped by column


Tag : python , By : user186435
Date : March 29 2020, 07:55 AM
hop of those help? You could mask the rate column in the dataframe, GroupBy the TYPE and transform with the mean, which will exlude NaNs. The use fillna to replace the values in the masked dataframe:
ma = df.rate.mask(df.rate.eq(0))
df['rate'] = ma.fillna(ma.groupby(df.TYPE).transform('mean').fillna(0))

   ID  TYPE  rate
0   1    A   2.0
1   2    B   2.0
2   3    C   1.0
3   4    A   2.0
4   5    C   1.0
5   6    C   3.0
6   7    C   8.0
7   8    C   2.0
8   9    D   0.0
Related Posts Related QUESTIONS :
  • Indexing all diagonals of a matrix in MATLAB
  • Rectify a photo in Matlab
  • How to calculate out put definite integral in matlab
  • Scope of recursively called function in Matlab
  • add white guassian noise to data with matlab
  • How do I append a character to my existing csv file?
  • Not enough input arguments for CDF
  • Need help vectorizing a loop in Matlab
  • how to use subplot within two loops
  • connect dots of scatterm
  • How to use a matrix to store a vector in each element
  • How to get the test case results from script?
  • Efficient ways to find minimum of subset of elements
  • How to get the total sum of several function calls in a double loop
  • MATLAB: Problem calling builtin('addlistener', ...)
  • NN training with Matlab: train() function output is not consistent with MSE error
  • Efficiently find Unique triplets of three char vectors in MATLAB
  • Make vector of elements less than each element of another vector
  • How to overlap a point cloud file with another plot in Matlab?
  • How to compare 2 matrices and keep greatest values in Matlab
  • Colour line plot by categorical data in MATLAB
  • how to do 3D number-density scatter plot using given data?
  • Obtain a table of proper size from a csv in Matlab
  • How to prevent the legend from updating in R2017a and newer?
  • How to replace groups of characters between flags in MATLAB
  • Is there an alternative of and() in Matlab that does not check syntax and returns a false as soon as the first false is
  • while loop continuously executing
  • How to empty a cell
  • How to use nested for loop with if else in the code to make it smaller
  • Histogram of an image
  • How to compute confidence intervals and plot them on a bar plot
  • How can I use dialog box in the mask editor to make my constant change?
  • Send data to serial port in little endian
  • I have a problem with this code. some idea?
  • Calculating Normalized Root Mean Square Error
  • matlab: get variable creation time
  • Breaking 64 figures into a set of subplots
  • How to fill 3d array with contents from two other 3d arrays with unequal first and second dimensions
  • Polymorphic Return Types in MATLAB
  • (In Matlab) How would I switch to another working directory in the middle of a for loop?
  • How to calculate distance from cluster centroids from the outputs of Matlab kmean() function
  • How can I force a variable in matlab to be a column vector
  • Looping Sortino Ratio in Matlab
  • Plot Command Does Not Show Anything
  • Creating an axis of months
  • how to covert 1/4 elements of a matrix to zero
  • How to check if an axes handle is cleared or not
  • Generating smaller video file size from frames
  • how to edit simulink plutosdr qpsk example
  • Get rectangle coordinates from a point and a line
  • after user had entered value update number to include commas
  • How to replace characters between flags in MATLAB
  • How to select from two identical bus signals in Simulink?
  • Difference between fspecial('sobel') and edge(i,'sobel') matlab
  • Insert a new line in listbox on Matlab GUIDE
  • creating url with sprintf creates wrong url
  • How to keep new symbolic value for subs remain through Matlab Code
  • I would like to beable to stack rows of one matrix in a special way
  • Two minimum values in matlab
  • MATLAB reconstruction of FFT 1D with an image
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com