Presentation describing the aggregate function in R
Ashu Guru
Author
03/13/2019
Added
155
Plays
Description
Presentation describing the aggregate function in R , by Ashu Guru, Univ of Nebraska Raikes School (4 mins)
Searchable Transcript
Toggle between list and paragraph view.
- [00:00:00.840]We now take a look at a function in R which is called as aggregate.
- [00:00:07.560]Suppose that we have data in a table form.
- [00:00:11.800]So suppose that you have data which is multiple columns.
- [00:00:16.600]In this case we have group on attribute 1 and attribute 2 and then we have a set of
- [00:00:22.602]rows for that data.
- [00:00:24.520]Further suppose that the data is stored in a CSV file.
- [00:00:27.960]So just to make our example simpler, I have it in a CSV file.
- [00:00:33.040]I'll show it to you here.
- [00:00:35.320]So here is my aggregate CSV dot TXT and I'll open it with a text editor and here
- [00:00:43.370]is that same data.
- [00:00:47.280]So I have 120, 20, 120, 20 and the last one is 101, 25, 18. 101,
- [00:00:53.418]25, 18.
- [00:00:56.200]So what we want is that we want to aggregate the rows in column attribute 1.
- [00:01:02.560]So we want to aggregate these rows based upon the common value in column group on
- [00:01:09.306]South.
- [00:01:09.800]We want to somehow aggregate them based on the column groupOns same value.
- [00:01:15.920]So in this case, we want to aggregate these 4 rows and
- [00:01:20.375]these two rows together.
- [00:01:22.320]So these two rows will have one row output,
- [00:01:24.938]these 4 rows will have one row output.
- [00:01:28.600]And the aggregate output must be the average value of the attribute 1.
- [00:01:33.680]So in this case, we want that the aggregated value should
- [00:01:37.652]be the average.
- [00:01:38.680]It could be the sum let's say.
- [00:01:40.520]But here we want to do an average value of it.
- [00:01:43.840]So in this case for 100 we have 20, 30, 35,
- [00:01:48.172]35 that's about 70 hundred and twenty so 120 divided by....
- [00:01:54.080]Since there are 1, 2, 3, 4 rows by 4 is 30 so we want the output
- [00:02:00.229]to be 3400 and for 101 it is 25 + 15 which is 40.
- [00:02:04.960]So the output should be 40 / 2 which is 20.
- [00:02:09.400]In order to perform this, we can use an R function which is called
- [00:02:13.353]as aggregate.
- [00:02:14.680]First setting my working directory to the folder where my CSV file aggregate CSV
- [00:02:20.718]TXT is.
- [00:02:21.240]Now I am loading the CSV file since it has a header,
- [00:02:25.327]header is true and it is a comma separated.
- [00:02:29.120]I'm loading into a variable called tab data.
- [00:02:32.040]I run that.
- [00:02:33.680]Now let me print column Groupon of the tab data.
- [00:02:38.760]So here it is.
- [00:02:41.000]So we see that the Groupon column has 100, 100, 101 and 101 as we had expected. 1, 2,
- [00:02:48.985]3, 4 hundreds, 2, 3, 4 hundreds and then 101 and 101.
- [00:02:54.280]And now I am going to apply the aggregate function on the list attributes.
- [00:03:02.720]So in this case in order to get the list reference I will say tab data and then in
- [00:03:08.957]square brackets attribute 1 and then I have to pass a list to the second
- [00:03:14.443]argument.
- [00:03:15.280]And in this case indexing should be based upon Groupon column of my tab data.
- [00:03:24.200]And then I want that the output is mean.
- [00:03:28.280]So once I do that I can print what is the value of aggregated mean.
- [00:03:34.000]My aggregated mean has a column My Grouped index value which has 100 and 101
- [00:03:40.431]and then it has the mean value based upon the common value in the column GroupOn.
The screen size you are trying to search captions on is too small!
You can always jump over to MediaHub and check it out there.
Log in to post comments
Embed
Copy the following code into your page
HTML
<div style="padding-top: 56.25%; overflow: hidden; position:relative; -webkit-box-flex: 1; flex-grow: 1;"> <iframe style="bottom: 0; left: 0; position: absolute; right: 0; top: 0; border: 0; height: 100%; width: 100%;" src="https://mediahub.unl.edu/media/10779?format=iframe&autoplay=0" title="Video Player: Presentation describing the aggregate function in R" allowfullscreen ></iframe> </div>
Comments
0 Comments