rm(list=ls())
getwd()
## [1] "C:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/Asif_06_2018"
ls()
## character(0)
setwd("C:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R")
dirpath <- "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data"
dir(dirpath, full=TRUE) # make sure the
## [1] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/ Figure .pdf"
## [2] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/countryCodes.csv"
## [3] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/crime.csv"
## [4] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/crime.txt"
## [5] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/datasets.csv"
## [6] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/GovType.csv"
## [7] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/HW1_US_Counties.csv"
## [8] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/movies.csv"
## [9] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/multiple.rdata"
## [10] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/pres1.csv"
## [11] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/sortCountyDecreasing.csv"
## [12] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/sortCountyDecreasing.Rda"
## [13] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/Tomato First.csv"
## [14] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/tomato.rdata"
## [15] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/USCounties_new.csv"
## [16] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/USPresidents.csv"
## [17] "c:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R/data/vehicles.csv"
getwd()
## [1] "C:/AGZ1/GD_AGZ1117/AGZ_Home/workspace_R"
list.files()
## [1] "-azhang1-2.Rhistory"
## [2] "-azhang1.Rhistory"
## [3] "-azhang3.Rhistory"
## [4] "2015_03_19 DataKindHackson-azhang1.R"
## [5] "2015_03_19 DataKindHackson.R"
## [6] "Asif_06_2018"
## [7] "BeginningR.html"
## [8] "BeginningR.Rmd"
## [9] "Cell.Rmd"
## [10] "Cell1.html"
## [11] "Cell1.Rmd"
## [12] "Class2Ex_GetStarted.knit.md"
## [13] "Cousera_Duket.R"
## [14] "Cousera_JHU"
## [15] "data"
## [16] "data-wrangling-cheatsheet.pdf"
## [17] "devtools-cheatsheet.pdf"
## [18] "Ebola_Outbreak.csv"
## [19] "genia"
## [20] "git_bitbucket_tutorial"
## [21] "h20.R"
## [22] "Hardy"
## [23] "Lecture2_GettingStartedWith.R"
## [24] "Lecture5_Visualization.R"
## [25] "ML_Homework1.Rmd"
## [26] "myfile"
## [27] "pres1.csv"
## [28] "projects"
## [29] "R in Action.R"
## [30] "README.md"
## [31] "RforRPC.Rmd"
## [32] "RforRPC_files"
## [33] "rmarkdown-cheatsheet.pdf"
## [34] "rmarkdown-reference-guide (1).pdf"
## [35] "Safari"
## [36] "Safari_Jared Lander.R"
## [37] "Safari_Jared Lander.Rmd"
## [38] "Shiny"
## [39] "shiny-cheatsheet.pdf"
## [40] "test-azhang3.R"
## [41] "test.R"
## [42] "test.R.out"
## [43] "Test_MD_2018.Rmd"
## [44] "tutorial"
## [45] "UCSC"
## [46] "USCounties_df_name.csv"
## [47] "USCounties_df_new.csv"
## [48] "USCounties_df_new.Rda"
## [49] "USCounties_name.csv"
## [50] "USCounties_new.csv"
## [51] "USCounties_new.Rda"
## [52] "USP.Rdata"
## [53] "USPresidents.csv"
## [54] "x.Rda"
## [55] "x.Rds"
myData<-read.csv("UCSC/MachinLearning/All_data/HW01pb1data.csv",header=FALSE) #
#verify:
head(myData)
## V1 V2 V3 V4 V5
## 1 0 0 0 10 0
## 2 10 0 10 0 10
## 3 30 0 40 50 20
## 4 0 10 10 10 20
## 5 20 50 10 20 40
## 6 10 0 100 0 10
names(myData)
## [1] "V1" "V2" "V3" "V4" "V5"
str(myData)
## 'data.frame': 800 obs. of 5 variables:
## $ V1: int 0 10 30 0 20 10 10 50 0 30 ...
## $ V2: int 0 0 0 10 50 0 0 10 10 20 ...
## $ V3: int 0 10 40 10 10 100 0 10 0 10 ...
## $ V4: Factor w/ 25 levels "0","10","100",..: 2 1 17 2 10 1 2 2 13 10 ...
## $ V5: Factor w/ 19 levels "0","10","120",..: 1 2 6 6 11 2 2 6 2 11 ...
x <- c(1,3,2,5)
x
## [1] 1 3 2 5
x = c(1,6,2)
x
## [1] 1 6 2
length (x)
## [1] 3
y = c(1,4,3)
y
## [1] 1 4 3
x+y
## [1] 2 10 5
ls()
## [1] "dirpath" "myData" "x" "y"
#?matrix()
length(x)
## [1] 3
length(y)
## [1] 3
# ?length
#Functions
ls()
## [1] "dirpath" "myData" "x" "y"
# The rm() function can be rm() used to delete any that we don't want.
rm(x,y)
ls()
## [1] "dirpath" "myData"
# It's also possible to remove all objects at once:
rm(list=ls())
list = ls()
list
## character(0)
matrix (c(1,2,3,4) ,2,2,byrow =TRUE)
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
x=matrix (c(1,2,3,4) ,2,2)
x
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
sqrt(x)
## [,1] [,2]
## [1,] 1.000000 1.732051
## [2,] 1.414214 2.000000
x^2
## [,1] [,2]
## [1,] 1 9
## [2,] 4 16
x=rnorm (50)
x
## [1] -0.24562579 -0.59992635 2.15732142 -0.19688450 1.55981799
## [6] -0.50417285 -0.23803021 0.76775823 0.22161833 -1.19314487
## [11] 1.00905389 -0.02639171 0.55094066 -1.15282490 0.36411843
## [16] 0.77643377 0.86756894 0.75825108 -0.55663371 -0.93086142
## [21] -0.01264997 0.45152218 -1.04302133 2.25490561 0.39399558
## [26] 1.29253732 1.66733927 -1.20761869 -0.12506339 1.77342061
## [31] -0.71407352 -0.42806212 0.96466947 0.25886839 -0.56472020
## [36] -0.52334300 -2.07832579 -0.73005819 1.55824901 -0.10210075
## [41] -0.14996537 -0.47974110 1.22117389 -0.08324590 -0.59139950
## [46] 1.21510441 -0.32480156 0.84579224 -1.76741430 -0.48980189
x=rnorm (50)
x
## [1] 0.64996055 -0.63727701 -0.47659923 -1.96826934 0.05586893
## [6] 0.22617433 -0.24623205 1.32359307 -0.63011273 -1.74425541
## [11] 0.72682451 2.09102389 0.28512073 1.15135845 0.37677992
## [16] 1.55270906 -0.19045067 -0.16272755 0.79062681 0.92313553
## [21] -0.85883325 -0.65608828 0.41925691 -1.26200014 -0.18747763
## [26] -2.46704605 0.92752319 0.57186872 0.14675322 -0.34408370
## [31] -0.63887953 -0.31382263 0.12542109 1.13003006 -0.52204727
## [36] 0.38915250 0.66988692 2.53693663 -1.09230781 -1.68271855
## [41] -0.33835243 0.16377185 0.49872444 1.82469401 -0.97597354
## [46] -0.05638009 -0.56787231 0.93016427 -0.19357448 -0.24192336
y=x + rnorm (50, mean=50, sd=.1)
y
## [1] 50.70001 49.46588 49.55037 48.07752 50.13757 49.99864 49.74195
## [8] 51.19288 49.23488 48.37032 50.72849 52.20967 50.38383 51.15315
## [15] 50.34436 51.55942 49.73481 49.97181 50.81732 51.00811 49.24312
## [22] 49.31795 50.49757 48.76051 49.84026 47.56358 50.92530 50.46197
## [29] 50.27878 49.57650 49.25223 49.69265 50.27766 51.18907 49.52255
## [36] 50.48684 50.74797 52.38706 49.05134 48.36095 49.70021 50.19429
## [43] 50.63189 51.89590 49.02859 49.95393 49.32547 50.84831 49.69899
## [50] 49.77269
cor(x,y)
## [1] 0.9962257
set.seed(3)
y=rnorm(10)
y
## [1] -0.96193342 -0.29252572 0.25878822 -1.15213189 0.19578283
## [6] 0.03012394 0.08541773 1.11661021 -1.21885742 1.26736872
mean(y)
## [1] -0.06713568
var(y)
## [1] 0.7494871
sqrt(var(y))
## [1] 0.8657293
sd(y)
## [1] 0.8657293
#####################
# 2.3.2 Graphics
#####################
x=rnorm (100)
y=rnorm (100)
plot(x,y)
plot(x,y,xlab=" this is the x-axis",ylab=" this is the y-axis", main=" Plot of X vs Y", color="red")
## Warning in plot.window(...): "color" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "color" is not a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "color" is not
## a graphical parameter
## Warning in axis(side = side, at = at, labels = labels, ...): "color" is not
## a graphical parameter
## Warning in box(...): "color" is not a graphical parameter
## Warning in title(...): "color" is not a graphical parameter
# pdf (" Figure .pdf ")
# ?pdf()
#dev.off ()
plot(x,y,col ="red")
#dev.off ()
seq(0,2,length=120)
## [1] 0.00000000 0.01680672 0.03361345 0.05042017 0.06722689 0.08403361
## [7] 0.10084034 0.11764706 0.13445378 0.15126050 0.16806723 0.18487395
## [13] 0.20168067 0.21848739 0.23529412 0.25210084 0.26890756 0.28571429
## [19] 0.30252101 0.31932773 0.33613445 0.35294118 0.36974790 0.38655462
## [25] 0.40336134 0.42016807 0.43697479 0.45378151 0.47058824 0.48739496
## [31] 0.50420168 0.52100840 0.53781513 0.55462185 0.57142857 0.58823529
## [37] 0.60504202 0.62184874 0.63865546 0.65546218 0.67226891 0.68907563
## [43] 0.70588235 0.72268908 0.73949580 0.75630252 0.77310924 0.78991597
## [49] 0.80672269 0.82352941 0.84033613 0.85714286 0.87394958 0.89075630
## [55] 0.90756303 0.92436975 0.94117647 0.95798319 0.97478992 0.99159664
## [61] 1.00840336 1.02521008 1.04201681 1.05882353 1.07563025 1.09243697
## [67] 1.10924370 1.12605042 1.14285714 1.15966387 1.17647059 1.19327731
## [73] 1.21008403 1.22689076 1.24369748 1.26050420 1.27731092 1.29411765
## [79] 1.31092437 1.32773109 1.34453782 1.36134454 1.37815126 1.39495798
## [85] 1.41176471 1.42857143 1.44537815 1.46218487 1.47899160 1.49579832
## [91] 1.51260504 1.52941176 1.54621849 1.56302521 1.57983193 1.59663866
## [97] 1.61344538 1.63025210 1.64705882 1.66386555 1.68067227 1.69747899
## [103] 1.71428571 1.73109244 1.74789916 1.76470588 1.78151261 1.79831933
## [109] 1.81512605 1.83193277 1.84873950 1.86554622 1.88235294 1.89915966
## [115] 1.91596639 1.93277311 1.94957983 1.96638655 1.98319328 2.00000000
x=seq (1,10)
x
## [1] 1 2 3 4 5 6 7 8 9 10
x=seq(-pi ,pi ,length =50)
x
## [1] -3.14159265 -3.01336438 -2.88513611 -2.75690784 -2.62867957
## [6] -2.50045130 -2.37222302 -2.24399475 -2.11576648 -1.98753821
## [11] -1.85930994 -1.73108167 -1.60285339 -1.47462512 -1.34639685
## [16] -1.21816858 -1.08994031 -0.96171204 -0.83348377 -0.70525549
## [21] -0.57702722 -0.44879895 -0.32057068 -0.19234241 -0.06411414
## [26] 0.06411414 0.19234241 0.32057068 0.44879895 0.57702722
## [31] 0.70525549 0.83348377 0.96171204 1.08994031 1.21816858
## [36] 1.34639685 1.47462512 1.60285339 1.73108167 1.85930994
## [41] 1.98753821 2.11576648 2.24399475 2.37222302 2.50045130
## [46] 2.62867957 2.75690784 2.88513611 3.01336438 3.14159265
y=x
f=outer(x,y,function (x,y)cos(y)/(1+x^2))
# glimpse(f)
contour (x,y,f)
contour (x,y,f,nlevels =45, add=T)
fa=(f-t(f))/2
contour (x,y,fa,nlevels =15)
image(x,y,fa)
persp(x,y,fa)
persp(x,y,fa ,theta =30)
persp(x,y,fa ,theta =30, phi =20)
persp(x,y,fa ,theta =30, phi =70)
persp(x,y,fa ,theta =30, phi =40)
### 2.3.3 Indexing Data
A=matrix (1:16 ,4 ,4) A A[2,3] A[c(1,3) ,c(2,4) ] A[1:3 ,2:4]
A[1:2 ,]
A[ ,1:2]
A[1,]
A[,1]
A[-c(1,3) ,]
A[-c(1,3) ,-c(1,3,4)]
dim(A)
### 2.3.4 Loading Data
```
LoadLibraries=function (){
library (ISLR)
library (MASS)
print ("The libraries have been loaded .")
}
LoadLibraries()
## [1] "The libraries have been loaded ."