Averaging values of unique ID's with duplicated dates

1 view (last 30 days)
I have a table of size 10743 x 5 but more interested in working with 3 columns namely; 3, 4 and 5.
I want to select the unique ID's in column 5, and find the averages of the corresponding value in column 3 with corresponding duplicated dates in column 4.
Please, how do I go about it?
Also, part of column 3 contains NaN values.
I have pasted part of the table as follows;
"AAE551" "02/02/1995" 228.35 728692 AAE551
"AAE551" "02/02/2006" 242.72 732710 AAE551
"AAE551" "02/20/2009" 246.05 733824 AAE551
"AAE551" "02/23/2010" 246.27 734192 AAE551
"AAE551" "02/26/1992" 226.60 727620 AAE551
"AAE551" "02/27/1991" 225.30 727256 AAE551
"AAE551" "03/05/1990" 222.70 726897 AAE551
"AAE551" "03/14/2006" 242.58 732750 AAE551
"AAE551" "03/21/2006" 242.72 732757 AAE551
"AAE551" "04/04/1990" 224.30 726927 AAE551
"AAE551" "04/09/2006" 242.81 732776 AAE551
"AAE551" "04/13/2006" 242.92 732780 AAE551
"AAE551" "05/02/1991" 224.70 727320 AAE551
"AAE551" "05/05/1993" 227.45 728054 AAE551
"AAE551" "05/06/2010" 246.33 734264 AAE551
"AAE551" "05/07/1990" 222.90 726960 AAE551
"AAE551" "05/07/1992" 226.33 727691 AAE551
"AAE551" "05/30/1989" 220.60 726618 AAE551
"AAE560" "02/04/1981" 46.300 723581 AAE560
"AAE560" "02/07/1980" 46.300 723218 AAE560
"AAE560" "02/10/1995" 42.820 728700 AAE560
"AAE560" "02/14/1986" 40.100 725417 AAE560
"AAE560" "02/17/1983" 44.800 724324 AAE560
"AAE560" "02/18/1987" 40.400 725786 AAE560
"AAE560" "02/25/1984" 42.000 724697 AAE560
"AAE560" "02/26/1988" 40.500 726159 AAE560
"AAE560" "02/28/1985" 40.700 725066 AAE560
"AAE560" "03/02/1992" 41.300 727625 AAE560
"AAE560" "03/04/1998" 37.650 729818 AAE560
"AAE560" "03/08/2006" 39.120 732744 AAE560
  4 Comments
Colkissi
Colkissi on 22 Jul 2019
Hi Trung,
Thanks for your quick response. However, I am new to Matlab so can you please run by me the coding itself?
Colkissi
Colkissi on 22 Jul 2019
Hi the cyclist'
I have attached the MAT file.
Thanks.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 22 Jul 2019
Edited: Adam Danz on 22 Jul 2019
We can't run your code since we do not have access to the csv file. We'd benefit more from having a mat file containing the table you're working with. Judging from the use of readtable(), it seems that you are working with tables rather than another data type but you haven't provided us with the header names so I will make them up.
T =
1×5 table
CodeStr DateStr Value Date Code
________ __________ ______ __________ ________
"AAE551" "02/02/95" 228.35 7.2869e+05 "AAE551"
"I want to select the unique ID's in column 5"
unqID = unique(T.Code);
"...and find the averages of the corresponding value in column 3 with corresponding duplicated dates in column 4. "
[groupID,GroupKey] = findgroups(T.Date);
groupMeans = splitapply(@(x)mean(x,'omitnan'),T.Value,groupID);
If you want to calculate the average "Value" for each date and for each 'Code', you use the lines below or use the lines above within a loop.
[~,~,groupID] = unique([T.Value, T.Code],'rows','stable');
groupMeans = splitapply(@(x)mean(x,'omitnan'),T.Value,groupID);
*This is untested
  8 Comments
Colkissi
Colkissi on 23 Jul 2019
Thank you Adam, this is exactly how I wanted it. I am sorry for taking much of your time, but then again I'm really grateful for that time spent to help me out.
I am new to Matlab and tried all I could but no results. Anyways, thanks again!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!