inst/doc/Week0_BasicR.R

## ----eval=FALSE---------------------------------------------------------------
#  variable <- value

## ----eval=FALSE---------------------------------------------------------------
#  class( variable )

## ----eval=FALSE---------------------------------------------------------------
#  ?help

## ----eval=FALSE---------------------------------------------------------------
#  vignette()

## -----------------------------------------------------------------------------
x <- 3
x

## -----------------------------------------------------------------------------
y <- 22/7
y

## -----------------------------------------------------------------------------
c(x,y)

## -----------------------------------------------------------------------------
x <- .3 / 3
x

## -----------------------------------------------------------------------------
print(x, digits=20)

## -----------------------------------------------------------------------------
rnorm(10)

## -----------------------------------------------------------------------------
rnorm(10,mean=42,sd=12)

## -----------------------------------------------------------------------------
rpois(10,lambda = 2)

## -----------------------------------------------------------------------------
rchisq(10, df=1)

## -----------------------------------------------------------------------------
x <- c("42","99")
x

## -----------------------------------------------------------------------------
y <- as.numeric( x )
y

## -----------------------------------------------------------------------------
x <- "The night is dark and full of terrors..."
as.numeric( x )

## -----------------------------------------------------------------------------
prof <- "Rodney J. Dyer"
prof


## -----------------------------------------------------------------------------
length(prof)

## -----------------------------------------------------------------------------
nchar(prof)

## -----------------------------------------------------------------------------
nchar(" \t ")

## -----------------------------------------------------------------------------
x <- "I am"
y <- "not"
z <- 'a looser'
terms <- c(x,y,z)
terms

## -----------------------------------------------------------------------------
length(terms)
nchar(terms)

## -----------------------------------------------------------------------------
paste(terms, collapse=" ")

## -----------------------------------------------------------------------------
paste(x,z)

## -----------------------------------------------------------------------------
paste(x,z,sep=" not ")

## -----------------------------------------------------------------------------
x <- 42
x

## -----------------------------------------------------------------------------
y <- as.character(x)
y

## -----------------------------------------------------------------------------
region <- c("North","North","South","East","East","South","West","West","West")
region <- factor( region )
region

## -----------------------------------------------------------------------------
region <- factor( region, levels=c("North","South","East","West","Central"))
region

## -----------------------------------------------------------------------------
region[1] <- "Bob"

## -----------------------------------------------------------------------------
region[1] <- "North"

## -----------------------------------------------------------------------------
region <- factor( region, ordered=TRUE, levels = c("West", "North", "South", "East") )
region

## -----------------------------------------------------------------------------
subregion <- region[ 3:9 ]
subregion

## -----------------------------------------------------------------------------
table( subregion )

## -----------------------------------------------------------------------------
canThrow <- c(FALSE, TRUE, FALSE, FALSE, FALSE)
canThrow

## -----------------------------------------------------------------------------
stable <- c( "RGIII" == 0, nchar("Marshawn") == 8)
stable

## -----------------------------------------------------------------------------
data <- rnorm(20)
data

## -----------------------------------------------------------------------------
data[ data > 0 ]

## -----------------------------------------------------------------------------
data > 0

## -----------------------------------------------------------------------------
1:20 %% 2

## -----------------------------------------------------------------------------
data[ (1:20 %% 2) > 0 ]

## -----------------------------------------------------------------------------
x <- c(1,2,3)
x
y <- c(TRUE,TRUE,FALSE)
y
z <- c("I","am","not","a","looser")
z

## -----------------------------------------------------------------------------
w <- c(TRUE, "1", pi, ls())
w
class(w)

## -----------------------------------------------------------------------------
x
x[1] <- 2
x[3] <- 1
x
x[2]

## -----------------------------------------------------------------------------
x <- 1:6
x

## -----------------------------------------------------------------------------
x <- 5:2
x

## -----------------------------------------------------------------------------
x <- 3.2:8.4
x

## -----------------------------------------------------------------------------
y <- seq(1,6)
y

## -----------------------------------------------------------------------------
z <- seq(1,20,by=2)
z

## -----------------------------------------------------------------------------
rep("Beetlejuice",3)

## -----------------------------------------------------------------------------
x <- c("No","Free","Lunch")
rep(x,time=3)

## -----------------------------------------------------------------------------
rep(x,each=3)

## -----------------------------------------------------------------------------
matrix(nrow=2, ncol=2)

## -----------------------------------------------------------------------------
matrix(1:4, nrow=2 )

## -----------------------------------------------------------------------------
matrix(1:4, nrow=2, byrow=TRUE)

## -----------------------------------------------------------------------------
Y <- matrix(c(1,2,3,4,5,6),ncol=2,byrow=TRUE)
Y

## -----------------------------------------------------------------------------
X <- matrix(c(1,2,3,4,5,6),nrow=2)
X

## -----------------------------------------------------------------------------
X == t(Y)

## -----------------------------------------------------------------------------
X <- matrix(c(1,2,3,4,5,6),ncol=4, byrow=TRUE)

## -----------------------------------------------------------------------------
X

## -----------------------------------------------------------------------------
dim(X)

## -----------------------------------------------------------------------------
X[1,3]

## -----------------------------------------------------------------------------
X[1,]

## -----------------------------------------------------------------------------
X[,3]

## -----------------------------------------------------------------------------
length(X[,3])

## -----------------------------------------------------------------------------
X[,2:3]

## ----eval=FALSE---------------------------------------------------------------
#  X[1,8]

## -----------------------------------------------------------------------------
indices <- cbind( rep(1:2, each=3), rep(1:3,times=2), rep(5,length.out=6)  )
indices

## -----------------------------------------------------------------------------
theList <- list( x=seq(2,40, by=2), dog=LETTERS[1:5], hasStyle=logical(5) )
summary(theList)

## -----------------------------------------------------------------------------
theList

## -----------------------------------------------------------------------------
theList$my_favoriate_number <- 2.9 + 3i

## -----------------------------------------------------------------------------
theList[["lotto numbers"]] <- rpois(7,lambda=42)

## -----------------------------------------------------------------------------
names(theList)

## -----------------------------------------------------------------------------
theList$x
theList$x[2] <- 42
theList$x

## -----------------------------------------------------------------------------
theList[["x"]]

## -----------------------------------------------------------------------------
theList[[2]]

## -----------------------------------------------------------------------------
theList[[1]][3]

## ----eval=FALSE---------------------------------------------------------------
#  theList[1,3]

## -----------------------------------------------------------------------------
df <- data.frame( ID=1:5, Names=c("Bob","Alice","Vicki","John","Sarah"), Score=100 - rpois(5,lambda=10))
df

## -----------------------------------------------------------------------------
df$Passed_Class <- c(TRUE,TRUE,TRUE,FALSE,TRUE)

## -----------------------------------------------------------------------------
summary(df)

## -----------------------------------------------------------------------------
df[1,3]

## -----------------------------------------------------------------------------
df[1,]

## -----------------------------------------------------------------------------
df[,3]

## -----------------------------------------------------------------------------
df$Names[3]

## -----------------------------------------------------------------------------
df[1:3,]

## -----------------------------------------------------------------------------
df$Names[df$Score > 90 & df$Passed_Class==TRUE]

## ----eval=FALSE---------------------------------------------------------------
#  function_name <- function( arguments ) { Stuff you want the function to do }

## ----message=FALSE, warning=FALSE---------------------------------------------
library(gstudio)
loc1 <- locus( c(128,130) )
loc2 <- locus( c(128,128) )
cat( loc1, loc2 )

## -----------------------------------------------------------------------------
off.alleles <- alleles( loc1 )
off.alleles
mom.alleles <- alleles( loc2 )
mom.alleles

## -----------------------------------------------------------------------------
shared <- intersect( off.alleles, mom.alleles )
shared

## -----------------------------------------------------------------------------
loc3 <- locus( c(132,132))
dad.alleles <- alleles( loc3 )
intersect( mom.alleles, dad.alleles )

## -----------------------------------------------------------------------------
potential_offspring <- function( parent, offspring ) {
  off <- alleles( offspring )
  par <- alleles( loc2 )
  shared <- intersect( off, par )
  return( length( shared ) > 0 )
}

## -----------------------------------------------------------------------------
potential_offspring(loc1, loc2)

## -----------------------------------------------------------------------------
potential_offspring(loc2, loc3)

## -----------------------------------------------------------------------------
x <- 10

## -----------------------------------------------------------------------------
do_it <- function( x ) {
  x <- x + 10
  return( x )
}

## ----eval=FALSE---------------------------------------------------------------
#  if( CRITERIA ) {
#  	DO_SOMETHING
#  }

## -----------------------------------------------------------------------------
is_heterozygote( c(loc1, loc2) )

## -----------------------------------------------------------------------------
if( is_heterozygote(loc1) ){
  print("It's a het!")
}

## -----------------------------------------------------------------------------
if( is_heterozygote(loc2) ){
  print("It's a het!")
}

## -----------------------------------------------------------------------------
if( is_heterozygote(loc1) ) {
  cat(loc1, "is a heterozygote")
} else {
  cat(loc1, "is a homozygote")
}

## -----------------------------------------------------------------------------
if( is_heterozygote(loc2) ) {
  cat(loc2, "is a heterozygote")
} else {
  cat(loc2, "is a homozygote")
}

## -----------------------------------------------------------------------------
ans <- ifelse( is_heterozygote( c(loc1, loc2)) , "heterozygote", "Not")
ans

## -----------------------------------------------------------------------------
which_is_het <- function( A, B) {
  if( is_heterozygote(A) & !is_heterozygote(B) ) {
    print("First is heterozygote")
  } else if( !is_heterozygote(A) & is_heterozygote(B) ){
    print("Second is heterozygote")
  } else if( is_heterozygote(A) & is_heterozygote(B) ){
    print("Both are heterozygotes")
  } else {
    print( "Neither are heterozygotes")
  }
}

## -----------------------------------------------------------------------------
x <- c(3,8,5,4,6)
x

## ----eval=FALSE---------------------------------------------------------------
#  for( SOME_SEQUENCE ){
#    DO_SOMETHING
#  }

## -----------------------------------------------------------------------------
for( val in x ){
  print(val)
}

## -----------------------------------------------------------------------------
for( i in 1:length(x)){
  print( x[i] )
}

## -----------------------------------------------------------------------------
y <- 1:5
for( i in 1:length(x)){
  if( x[i] %% 2)
    print( x[i] + y[i])
  else
    print( x[i] - y[i] )
}

## -----------------------------------------------------------------------------
for( i in 1:20 ){
  if( i %% 3 )
    next
  cat("The value of i =",i,"\n")
}

## -----------------------------------------------------------------------------
for( i in 1:10){
  if( i > 2 )
    break
  cat("The value of i=",i,"\n")
}

## ----message=FALSE, warning=TRUE, include=FALSE-------------------------------
LandGenCourse::detachAllPackages()
hhwagner1/LandGenCourse documentation built on March 25, 2020, 5:34 p.m.