R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

data<-c(1,2,3,4,5,6)
data
## [1] 1 2 3 4 5 6
vString<-c('a','b','c')
vString
## [1] "a" "b" "c"
headings<-list(NULL, c("a", "b", "c"))
headings
## [[1]]
## NULL
## 
## [[2]]
## [1] "a" "b" "c"
m<-matrix(data, nrow=2, ncol=3,byrow=TRUE, dimnames=headings)
m
##      a b c
## [1,] 1 2 3
## [2,] 4 5 6
m2<-matrix(data, nrow=2, ncol=3,byrow=FALSE, dimnames=headings)
m2
##      a b c
## [1,] 1 3 5
## [2,] 2 4 6
m3<-matrix(data, nrow=3, ncol=3,byrow=TRUE, dimnames=headings)
## Warning in matrix(data, nrow = 3, ncol = 3, byrow = TRUE, dimnames = headings):
## data length differs from size of matrix: [6 != 3 x 3]
m3[1,]
## a b c 
## 1 2 3
m3[,2]
## [1] 2 5 2
m3[3,2]
## b 
## 2
a<- list(aa=1, bb=2, cc=3)
a
## $aa
## [1] 1
## 
## $bb
## [1] 2
## 
## $cc
## [1] 3

To write comments and see them too

This is how to add comments to your code to document it.

years <- c(1980, 1980, 1985, 1990)

scores <- c(34, 44,56,  83)

df <- data.frame(years, scores)
df[,1]
## [1] 1980 1980 1985 1990
df$years
## [1] 1980 1980 1985 1990
df$scores
## [1] 34 44 56 83
df$initial=c('d','b','s','u')

df1<-df[df$scores< 50,]
df1
##   years scores initial
## 1  1980     34       d
## 2  1980     44       b
a <- 66
if (a > 55) {print("a is more than 55") } else {print("a is less than or equal to 55")}
## [1] "a is more than 55"
mylist <- c(55, 66, 77, 88, 99)
for (value in mylist) {
    print(value)
}
## [1] 55
## [1] 66
## [1] 77
## [1] 88
## [1] 99
a <- 100
while (a < 500) {
    a <- a + 100
    print(a)
}
## [1] 200
## [1] 300
## [1] 400
## [1] 500
a
## [1] 500
sqrt(24)
## [1] 4.898979
sqrt(c(23,24,25))
## [1] 4.795832 4.898979 5.000000

Including Plots

You can also embed plots, for example:

convMeters <- function(val, to="inch") 
  {  mult<- switch(to,inch=39.3701,foot=3.28084,yard=1.09361,mile=0.000621371, cm=100, NA) 
  if (is.na(mult)) print("Unknown target unit of length.") else return(val*mult)}
convMeters(23,"foot")
## [1] 75.45932
Inches= convMeters(40,"inch")
sprintf("40 meters are equivalent to %f  inches", Inches)
## [1] "40 meters are equivalent to 1574.804000  inches"
convMeters(40)
## [1] 1574.804
convMeters(5, "cm")
## [1] 500
convMeters(2.4,"km")
## [1] "Unknown target unit of length."
set.seed(30)
x <- rnorm(50)
x
##  [1] -1.28851820 -0.34768941 -0.52162885  1.27347316  1.82452060 -1.51130794
##  [7]  0.11050805 -0.76079623 -0.66989702  0.27451969 -1.02327202 -1.81939791
## [13] -0.66778981 -0.05929799  0.88016591  0.26851292 -0.01957938 -0.52494697
## [19] -1.40933143 -1.83398921 -0.15831429  0.75442657 -0.91212962  0.79993114
## [25]  1.49055300 -1.09640199 -0.53422069 -1.42120303 -1.24273830  0.23193618
## [31] -1.72520250  0.61486070  0.72687514 -0.04219020  0.21600180  1.76973639
## [37]  0.22035091  0.53147867  2.16970096 -2.93441824 -0.99556463  1.16955322
## [43] -0.48003984 -1.66886763  1.13399719 -0.31759583  0.17585799 -0.62550955
## [49] -1.63952782 -0.67148442
y= mean(x)
y
## [1] -0.2457178
x2<-rnorm(50, mean=50, sd=.1)
x2
##  [1] 49.91510 50.01232 49.93803 49.82863 50.07158 49.90845 50.25984 49.94884
##  [9] 49.91336 50.03872 50.09741 50.03992 50.03483 49.97095 50.15766 50.08463
## [17] 49.90806 50.04006 50.03544 50.16578 50.11148 49.92594 50.20109 49.99744
## [25] 49.99148 50.05016 49.92748 50.11054 49.93674 49.98028 50.08515 49.94338
## [33] 49.98478 49.95711 50.22187 49.79664 49.89340 50.15721 49.92696 50.04505
## [41] 50.05644 49.97782 49.95307 50.05727 49.87068 50.02613 50.01002 50.02696
## [49] 50.02119 49.86163

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.