472,327 Members | 1,155 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,327 developers and data experts.

Lesson 4 (part2) – data management and visualization

nbiswas
149 100+
Sorting data in a vector is quite easy in R. You simply pass the vector to the sort function and new sorted vector is generated.
The default sort order is ascending, but this can be changed using the argument decreasing=TRUE.
However, in our scenario the data is not in a vector, it is in a data frame. Let's examine how we sort data stored contained in a data frame.
The data frame n.2010 contains three columns: Year, Name and Frequency.
There is no need to keep the Year attribute so we decide to filter our data frame to remove the Year column.
Here is an example using the bracket notation of how we can filter out the Year column by specifying a character vector of column names within the brackets.
If you are familiar with SQL, this task would be equivalent to using projection to specify the columns in the SELECT list for an SQL statement.
We use the head() function to verify that the Year column has been removed. The function shows that the data is still in its original sort order by name.
To find the most popular names we must sort by Frequency in decreasing or descending order.
We can't use the function sort as we did with vectors, but with data frames we will use the order function within our bracket notation.
Here we want the data sorted by decreasing frequency values and all of the columns should be included in our output.
The final expression converts the Factor vector into a simple character vector to determine that the most popular name was: Ethan, followed by Liam, Jacob, Lucas, and Noah.
Whenever we have explored a data set and wish to save it for future reference we can use one of the write() family of functions.
Here we see some examples of exporting the sorted male names from the year 2010 to a comma separated values or csv file and also to a tab delimited file.
In many data frames the row names are not really used to identify data so we have used the row.names=FALSE option to avoid writing the row name information in our output file.
So far we have worked with data within data frames. Now let's visualize trends in our data using Data Visualizations or graphics.
In this task we would like to provide an interactive experience so that the user can provide a baby name and then view a single visualization of its popularity from 1917 to 2010.

Let's get started.
So far we have imported data from external files, but we have not obtained input from users before.
We can use the readline() function to read data from text files, websites, or in this example we will use the function to obtain data from the standard input device or keyboard. We specify the prompt with the optional argument.
To visualize trends in baby names we would decide to use a scatter plot visualization based on frequencies over the years 1917 to 2010.
To do this we create a new data frame which is a subset of frequencies for the name provided by the user. The built-in toupper() function is used to convert the input character string to all uppercase so we can match the data stored in the data.frame. We also only require the two variables of Year and Frequency. If you are familiar with SQL you can imagine how this query would look using an SQL SELECT statement, but in R we do things a bit different.
We then sort the data within the data frame and call the plot() function.
The plot function uses the values provided to generate a visualization.
Plot is an example of a generic function in R as it can accept many different data formats and it will attempt to create a visualization.
Let's take a look at the visualization.
When the data is provided to the plot function as a data frame of 2 variables, plot will create a scatterplot using circles for each observation.
Notice how default labels and scales were generated.
The x and y axis labels are determined based on the defined column names.
We will examine a few additional commonly used options for the plot() function next.
Most visualizations should have a title. The main argument is used to provide a title for a visualization.
The label names can be specified for each axis and the method of plotting can be changed from the default to lines, stairs, or other representations.
Let's look at another example with our baby name data frame.
This plot uses the same data, but it uses the paste function to construct a more precise title for our chart and the type of plot is changes from using circles to using stairs.
R has been able to help us spot trends in our data set across 2 variables. It is fairly obvious from this data that the first name or given name of "GRANT" was most popular in the early 1960s and from our earlier analysis we know that it was definitely not one of the top 5 names of newborns in 2010.
This plot uses the same data, but it uses the paste function to construct a more precise title for our chart and the type of plot is changes from using circles to using stairs.
R has been able to help us spot trends in our data set across 2 variables. It is fairly obvious from this data that the first name or given name of "GRANT" was most popular in the early 1960s and from our earlier analysis we know that it was definitely not one of the top 5 names of newborns in 2010.
Let's look at one more example of trend analysis and also learn a few more plotting options along the way.
In this dataset we plan to examine the maximum and minimum temperatures during the month of July 2013 in Toronto.
After the data is loaded we check the structure of the data frame and decide to move forward.
Note that there are some missing values of wind speed in our data, but we will only examine temperatures so we can ignore this variable at this time.
We would like our graph to include both maximum and minimum values for each day of the month.
Therefore, we need to determine the range of values to set our axis properly.
Here we have created a variable called yrange that contains the lowest temperature and the highest temperature for the month.
As we plot the data we will specify a line graph type with a line width of 6 pixels and a colour of blue. The y-axis limit is specified using our yrange vector and we have also included a title in our initial plot.
At this point we are not finished as we now wish to add the maximum temperatures and our axis labels to the same plot.
The par() function can be used to control various features for our graph. Here we are telling R that our next plot() function call should reuse the existing plot.
Now we add the red line of maximum temperatures for the month. We also decide to add the x and y axis labels at this time.
The final step is to include the axis range of values to our chart.
R has helped us explore how July was very warm in the middle of July followed by an extended cool period lasting until the end of the month.
We can use summary statistics to determine the mean or average temperature in the month of July or examine trends like we did in the previous example.
Now we would like to examine the distribution of temperatures during the month. A distribution of values are often visualized using a histogram were the data is grouped into categories and the categories are visualized.
Here is an example that shows the most frequent maximum temperature in July was between 22 and 24 degrees Celsius.
The breaks argument can be used to determine the size of the categories.
So now if someone asks what was it like in July 2013 in Toronto you will have lots to discuss based on learning R.
R has many more data visualization capabilities.
One of the most popular R extensions for graphics is called ggplot2 by Hadley Wickham and if your goal is to use R to create data visualizations then exploring the ggplot2 package is a good idea.
Sep 4 '14 #1
1 4322
zmbd
5,501 Expert Mod 4TB
nbiswas:
Please provide proper citations for these articles.
-z
Sep 29 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data...
0
by: Matthieu Siggen | last post by:
Hello, I'm really confused about how to define services when concerning data management. I'm going to take an example to show where is my...
0
by: Enorme Vigenti | last post by:
Hi all, I have a problem with sqlserver 2000 and large data management. I have a database with a large tables. Every table has a continuative input...
2
by: Bryan.Fodness | last post by:
I would like to have my data in a format so that I can create a contour plot. My data is in a file with a format, where there may be multiple...
3
by: Karabo | last post by:
What Are The Features And Data Management Strategies Of Postgresql
1
nbiswas
by: nbiswas | last post by:
Welcome to the lesson on R data structures. To perform any meaningful data analysis we need to collect our data into R data structures. In this...
1
nbiswas
by: nbiswas | last post by:
In this lesson we will learn how to import external data from files into R. We will also learn how to export, or write, the data to files if needed. ...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.