Updated on Tue May 16 20:57:22 2017.
This section will guide you in the process of decoding your data into information and ultimately intelligible insights. In doing so, we will explore the use of tidyverse and R base packages.
When working with a new data what initial questions do you have?
Consider the following questions to guide your understanding.
Once you have this basic understanding of your data you can dig deeper. Then you can use visualization techniques to explore your data and derive some basic understandings of the phenomena you are studying, such as the largest and smallest values for each variable. In addition, calculating summary statistics translate data into information by revealing the shape of the data, the mean, median, minimum value, maximum value, and variability all with simple visualizations.
For any data science project there are few simple steps to follow.
Using the World internet usage data we will compare of read.csv to read_csv for importing data.
internet_utils <- read.csv("world_internet_usage.csv")
head(internet_utils)
## country X2000 X2001 X2002 X2003 X2004 X2005 X2006 X2007
## 1 China 1.78 2.64 4.60 6.20 7.30 8.52 10.52 16.00
## 2 Mexico 5.08 7.04 11.90 12.90 14.10 17.21 19.52 20.81
## 3 Panama 6.55 7.27 8.52 9.99 11.14 11.48 17.35 22.29
## 4 Senegal 0.40 0.98 1.01 2.10 4.39 4.79 5.61 7.70
## 5 Singapore 36.00 41.67 47.00 53.84 62.00 61.00 59.00 69.90
## 6 United Arab Emirates 23.63 26.27 28.32 29.48 30.13 40.00 52.00 61.00
## X2008 X2009 X2010 X2011 X2012
## 1 22.60 28.90 34.30 38.30 42.30
## 2 21.71 26.34 31.05 34.96 38.42
## 3 33.82 39.08 40.10 42.70 45.20
## 4 10.60 14.50 16.00 17.50 19.20
## 5 69.00 69.00 71.00 71.00 74.18
## 6 63.00 64.00 68.00 78.00 85.00
library(readr)
internet_readr <- read_csv("world_internet_usage.csv")
## Parsed with column specification:
## cols(
## country = col_character(),
## `2000` = col_double(),
## `2001` = col_double(),
## `2002` = col_double(),
## `2003` = col_double(),
## `2004` = col_double(),
## `2005` = col_double(),
## `2006` = col_double(),
## `2007` = col_double(),
## `2008` = col_double(),
## `2009` = col_double(),
## `2010` = col_double(),
## `2011` = col_double(),
## `2012` = col_double()
## )
head(internet_readr)
## # A tibble: 6 × 14
## country `2000` `2001` `2002` `2003` `2004` `2005` `2006`
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 China 1.78 2.64 4.60 6.20 7.30 8.52 10.52
## 2 Mexico 5.08 7.04 11.90 12.90 14.10 17.21 19.52
## 3 Panama 6.55 7.27 8.52 9.99 11.14 11.48 17.35
## 4 Senegal 0.40 0.98 1.01 2.10 4.39 4.79 5.61
## 5 Singapore 36.00 41.67 47.00 53.84 62.00 61.00 59.00
## 6 United Arab Emirates 23.63 26.27 28.32 29.48 30.13 40.00 52.00
## # ... with 6 more variables: `2007` <dbl>, `2008` <dbl>, `2009` <dbl>,
## # `2010` <dbl>, `2011` <dbl>, `2012` <dbl>
#extract by position
internet_readr[[2,1]]
## [1] "Mexico"
internet_utils [2,1] # double [[ ]] works too
## [1] Mexico
## 7 Levels: China Mexico Panama Senegal Singapore ... United States
#extract by name
internet_readr$country
## [1] "China" "Mexico" "Panama"
## [4] "Senegal" "Singapore" "United Arab Emirates"
## [7] "United States"
internet_utils$country
## [1] China Mexico Panama
## [4] Senegal Singapore United Arab Emirates
## [7] United States
## 7 Levels: China Mexico Panama Senegal Singapore ... United States
#to use with infix function add a .
internet_readr %>% .$country
## [1] "China" "Mexico" "Panama"
## [4] "Senegal" "Singapore" "United Arab Emirates"
## [7] "United States"
You need to rename columns first to remove the X in front of each year.
names(internet_utils) <-c("country", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012")
names(internet_utils)
## [1] "country" "2000" "2001" "2002" "2003" "2004" "2005"
## [8] "2006" "2007" "2008" "2009" "2010" "2011" "2012"
Reshape a data frame
library(reshape2)
internet_utils_reshaped <- melt(internet_utils,id.vars="country", variable.name="year", value.name="usage")
Reshape a tibble
internet_readr_reshaped <- melt(internet_readr,id.vars="country", variable.name="year", value.name="usage")
internet_readr_reshaped
## country year usage
## 1 China 2000 1.78
## 2 Mexico 2000 5.08
## 3 Panama 2000 6.55
## 4 Senegal 2000 0.40
## 5 Singapore 2000 36.00
## 6 United Arab Emirates 2000 23.63
## 7 United States 2000 43.08
## 8 China 2001 2.64
## 9 Mexico 2001 7.04
## 10 Panama 2001 7.27
## 11 Senegal 2001 0.98
## 12 Singapore 2001 41.67
## 13 United Arab Emirates 2001 26.27
## 14 United States 2001 49.08
## 15 China 2002 4.60
## 16 Mexico 2002 11.90
## 17 Panama 2002 8.52
## 18 Senegal 2002 1.01
## 19 Singapore 2002 47.00
## 20 United Arab Emirates 2002 28.32
## 21 United States 2002 58.79
## 22 China 2003 6.20
## 23 Mexico 2003 12.90
## 24 Panama 2003 9.99
## 25 Senegal 2003 2.10
## 26 Singapore 2003 53.84
## 27 United Arab Emirates 2003 29.48
## 28 United States 2003 61.70
## 29 China 2004 7.30
## 30 Mexico 2004 14.10
## 31 Panama 2004 11.14
## 32 Senegal 2004 4.39
## 33 Singapore 2004 62.00
## 34 United Arab Emirates 2004 30.13
## 35 United States 2004 64.76
## 36 China 2005 8.52
## 37 Mexico 2005 17.21
## 38 Panama 2005 11.48
## 39 Senegal 2005 4.79
## 40 Singapore 2005 61.00
## 41 United Arab Emirates 2005 40.00
## 42 United States 2005 67.97
## 43 China 2006 10.52
## 44 Mexico 2006 19.52
## 45 Panama 2006 17.35
## 46 Senegal 2006 5.61
## 47 Singapore 2006 59.00
## 48 United Arab Emirates 2006 52.00
## 49 United States 2006 68.93
## 50 China 2007 16.00
## 51 Mexico 2007 20.81
## 52 Panama 2007 22.29
## 53 Senegal 2007 7.70
## 54 Singapore 2007 69.90
## 55 United Arab Emirates 2007 61.00
## 56 United States 2007 75.00
## 57 China 2008 22.60
## 58 Mexico 2008 21.71
## 59 Panama 2008 33.82
## 60 Senegal 2008 10.60
## 61 Singapore 2008 69.00
## 62 United Arab Emirates 2008 63.00
## 63 United States 2008 74.00
## 64 China 2009 28.90
## 65 Mexico 2009 26.34
## 66 Panama 2009 39.08
## 67 Senegal 2009 14.50
## 68 Singapore 2009 69.00
## 69 United Arab Emirates 2009 64.00
## 70 United States 2009 71.00
## 71 China 2010 34.30
## 72 Mexico 2010 31.05
## 73 Panama 2010 40.10
## 74 Senegal 2010 16.00
## 75 Singapore 2010 71.00
## 76 United Arab Emirates 2010 68.00
## 77 United States 2010 74.00
## 78 China 2011 38.30
## 79 Mexico 2011 34.96
## 80 Panama 2011 42.70
## 81 Senegal 2011 17.50
## 82 Singapore 2011 71.00
## 83 United Arab Emirates 2011 78.00
## 84 United States 2011 77.86
## 85 China 2012 42.30
## 86 Mexico 2012 38.42
## 87 Panama 2012 45.20
## 88 Senegal 2012 19.20
## 89 Singapore 2012 74.18
## 90 United Arab Emirates 2012 85.00
## 91 United States 2012 81.03
class(internet_readr_reshaped) # turns into a data.frame!
## [1] "data.frame"
Use the gather function to reshape
tidy_internet_readr <-
internet_readr %>%
gather(`2000`,`2001`,`2002`,`2003`,`2004`,`2005`,`2006`,`2007`,`2008`,`2009`,`2010`,`2011`,`2012`, key="year", value="usage")
tidy_internet_readr
## # A tibble: 91 × 3
## country year usage
## <chr> <chr> <dbl>
## 1 China 2000 1.78
## 2 Mexico 2000 5.08
## 3 Panama 2000 6.55
## 4 Senegal 2000 0.40
## 5 Singapore 2000 36.00
## 6 United Arab Emirates 2000 23.63
## 7 United States 2000 43.08
## 8 China 2001 2.64
## 9 Mexico 2001 7.04
## 10 Panama 2001 7.27
## # ... with 81 more rows
Create a few statistical visualizations to understand the makeup of your data.
boxplot(internet_readr$`2000`, main="Range of internet users in 2000", sub="Median of 6.55 users per 100 people")
hist(internet_readr$`2000`, main="Frequency of internet users in 2000 per 100 people", xlab="2000")
library(lattice)
histogram(internet_readr$`2000`, main="Frequency of internet users in 2000 per 100 people", xlab="2000")
boxplot(internet_readr[,2:14], main="Range of internet users per 100 people")
plot(tidy_internet_readr$year, tidy_internet_readr$usage,main="Internet usage per 100 people",xlab="Year",ylab="Usage", type="p")
***
Create charts and reports.
library(ggthemes)
library(ggplot2)
#scatter plot
ggplot(tidy_internet_readr,aes(x=year,y=usage,colour=country,group=country)) + geom_line() + labs(title = "Internet Usage per 100 people", subtitle = "Since 2011, the UAE has surpassed Singapore and the US in internet users", caption = "Source: World Bank, 2013",x = "Year",y ="Usage") + theme_few()
Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents as webpages, interactive apps, and slides.
For more details on using R Markdown see http://rmarkdown.rstudio.com.