# By Luis George
# We show how to manipulate data in R
# learn to change name of variable in a column
# plot using lattice
# the data shows life expectancy by region
setwd("C:\\Users\\Luis\\Documents\\Making sense of data")
lifesp <-read.table("C:\\Users\\Luis\\Documents\\Making sense of data\\LifeExpRegion.txt",
sep="")
names(lifesp)
## [1] "V1" "V2" "V3"
attach(lifesp)
library(plyr)
lifesp <-rename(lifesp,c("V1"="country","V2"="lifespx","V3"="region"))
names(lifesp)
## [1] "country" "lifespx" "region"
lifesp[1:5,]
##       country lifespx region
## 1 Afghanistan  48.673    SAs
## 2     Albania  76.918   EuCA
## 3     Algeria  73.131   MENA
## 4      Angola  51.093    SSA
## 5   Argentina  75.901   Amer
str(lifesp)
## 'data.frame':    197 obs. of  3 variables:
##  $ country: Factor w/ 197 levels "Afghanistan",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ lifespx: num  48.7 76.9 73.1 51.1 75.9 ...
##  $ region : Factor w/ 6 levels "Amer","EAP","EuCA",..: 5 3 4 6 1 3 1 2 3 3 ...
library(lattice)
histogram(~lifespx|factor(region),data = lifesp)

densityplot(~lifespx,data = lifesp,groups = region,
            plot.points=FALSE,
            auto.key = list(space="right",title="region"))