x x[2] x[3] save.image( "obay.RData" ) x <- 42 x load( "obay.RData" ) x x } } return(myVector); return(arg.1); } return(42) # comment return(42) other.function <- function(arg.1, ...) { kl } my.function(42) my.function(42) my.function(42) my.function <- function(argument.1, argument.2, ...) { # clever manipulations of arguments return(42) } my.function <- function(argument.1, argument.2, ...) { # clever manipulations of arguments return(42) } my.function(42) MyMode <- function(arg.1) { # clever manipulations of arguments return(arg.1) } MyMode( c(1,3,4)) MyMode <- function(arg.1) { # clever manipulations of arguments uniqueValues <- unique(myVector) return(uniqueValues) } MyMode <- function(arg.1) { # clever manipulations of arguments uniqueValues <- unique(myVector) return(uniqueValues) } MyMode <- function(arg.1) { # clever manipulations of arguments uniqueValues <- unique(myVector) return(uniqueValues) } x x <- c(x,x,4) x MyMode(x) MyMode <- function(myVector) { # clever manipulations of arguments uniqueValues <- unique(myVector) return(uniqueValues) } MyMode <- function(myVector) { # clever manipulations of arguments uniqueValues <- unique(myVector) return(uniqueValues) } MyMode(x) ?tabulate tabulate(x) source('~/.active-rstudio-document', echo=TRUE) ?match tabulate( c(0,0,1,2)) tabulate( c(0,0,1,2,8,9)) d <- c(1,2,1,2,3,3,3,4,5,4,5,5,5,5,1,1,1) e <- c(d,9,9,9,9,9,9,9,9) e tabulate(d) d tabulate(e) match(e) match( c(1,2,9), c(9,2,1) ) match( c(1,2,9,2,9), c(9,2,1) ) e match( e, unique(e) ) tabulate(match( e, unique(e) ) ) install.packages("modeest") install.packages("modeest") library(modeest) e mfv(e) mfv factor(e) f <- factor(e) f tf <- tabulate(f) tf tf == max(tf) levels(f) ?factor ?levels myFamilyNames <- c("dad", "mom", "sis", "bro","dog") myFamilyNames mfAges <- c(43,42,12,8,5) mfAges mf <- data.frame( myFamilyNames, mfAges ) View(mf) mf mf$myFamilyNames mf$mfAges factors(mf$mfAges) factor(mf$mfAges) _ f <- factor(mf$mfAges) levels(f) as.numeric( levels(f) ) str(mf) mfGenders = c("Male","Female","Female","Male","Male") ?cbind m <- cbind(1, 1:7) M m mf mf <- cbind( mf, mfGenders ) mf View(mf) View(mf) mf$mfGenders mode(mf) mode(mf$mfGenders) str(mf) summary(mf) mf rbind( mf, c("cat","3","Female")) View(mf) View(mf) mf mf[[1]] mf[[2]] mf[1] mf[2] ?[[]] seq(5) iri iris names(iris) ir <- iris[1:2,] ir ir#Sepal.Length ir$Sepal.Length ir[Sepal.Length] ir["Sepal.Length"] ir[1] ir[1,2] ir[2,1] ir[1][2] ir[1] ir[1][2] ir[1][[2]] t <- ir[1] t t[1,1] t[2,1] ir ir[-2] str(ir) iris[iris$Species == "setosa"] iris[iris$Species == "setosa",] iris[iris$Species == "setosa",] ir ir <- iris[1:10,] ir ir[ir$Sepal.Length >= 5.0, ] ir[ir$Sepal.Length >= 5.0 & Sepal.Width<3.8, ] ir[ir$Sepal.Length >= 5.0 & ir$Sepal.Width<3.8, ] ir[ir$Sepal.Length >= 5.0 | ir$Sepal.Width<3.8, ] ir ir[ c(F,T), ] ir ir$Sepal.Length <- c(ir$Sepal.Length,4.2) ir View(m) View(m) ?del ?rm rm(m) ctl ir[ c(F,T), ] str(mf) mf mfa <- mfAges mfa mfa[1] <- 44 mfa mfAges mfa <- mfAges var(mfa) sd(mfa) ?sqrt read.DIF("clipboard",transpose=T) read.DIF("clipboard",transpose=T) sp <- read.DIF("clipboard",transpose=T) sp sp1 <- sp$V1 sp1 1:10 1:10 * 2 sum(1:10) 1:10 * 10:1 1:10 * 2 sp mode(sp) sp[ sp$V1 > 1e6] sp[ sp$V1 > 1000000] sp[1:4] sp[ sp$V1 > 1000000,] sp[ sp$V1 > 1e6,] sp[ sp$V1 > 1e7,] sp1 mean(sp1) median(sp1) sd(sp1) hist(sp1) hist(sp1,breaks=20) ?gnorm qgeom((1:9)/10, prob = .2) Ni <- rgeom(20, prob = 1/4); table(factor(Ni, 0:max(Ni))) rgeom(100,prob=.5) rgeom(100,prob=.1) ?sample sp1 sample( sp1, size=16, replace=T) mean(sp1) mean(sample( sp1, size=16, replace=T)) mean(sample( sp1, size=16, replace=T)) mean(sample( sp1, size=160, replace=T)) mean(sample( sp1, size=160, replace=T)) mean(sample( sp1, size=160, replace=T)) mean(sample( sp1, size=1600, replace=T)) mean(sample( sp1, size=1600, replace=T)) mean(sample( sp1, size=16, replace=T)) ?replicate replicate(4, mean(sample( sp1, size=16, replace=T))) mean(replicate(4, mean(sample( sp1, size=16, replace=T)))) mean(replicate(40, mean(sample( sp1, size=16, replace=T)))) mean(replicate(40, mean(sample( sp1, size=16, replace=T)))) mean(replicate(400, mean(sample( sp1, size=16, replace=T)))) mean(replicate(4000, mean(sample( sp1, size=16, replace=T)))) mean(sp1) hist(replicate(4000, mean(sample( sp1, size=16, replace=T)))) sm <- replicate(10000, mean(sample( sp1, size=16, replace=T))) length(sm) str(sm) summary(sm) hist(sm) ?quantile quantile(sm) quantile(sm,probs=c(0,1,.25)) quantile(sm,probs=seq(0,1,.25)) seq(0,1,.25) quantile(sm,probs=seq(0,1,.2)) quantile(sm,probs=seq(0,1,.1)) median(sm) hist(sm) hist(sm,breaks=30) hist(sm,breaks=30,col=green) hist(sm,breaks=30,col="green) "" " hist(sm,breaks=30,col="green") download.file sqrt co2 Presige Prestige prestige data() uspop Prestige str(uspop) plot(uspop) swiss sunspots ?aggregate 24 . x<-24 . 891-742 ??csv getwd() getwd() ?cd ?setwd setwd("C:/Users/Owner/Desktop") getwd() ?read.csv t <- read.csv("train.csv") View(t) summary(t) predictor <- function(obs) { return(0) } pr <- function(obs) { return(0) } head(t) t2 <- t[1:6,] t2 pr(t2[1,]) t2[1,] ?lapply lapply(t2,pr) ?predict predict t2 t2$sex t2[1,] pr <- function(obs) { if (obs$sex == "female") { return(1) } else { return(0) } } t2 pr( t2[1,]) pr( t2[2,]) # full data-frame prediction... df_pr(FUN,df) { L <- length(df) print(L) return(42) } # full data-frame prediction... df_pr <- function(FUN,df) { L <- length(df) print(L) return(42) } df_pr(pr,t2) length(t2) t2 str(t2) nobs nobs(t2) ?nobs ??nobs ?nrows ??rows length(t2[1]) length(t2[[1]]) ?concatenate cat("hi", 42) # full data-frame prediction... df_pr <- function(FUN,df) { NUM_ROWS <- length(df[[1]]) cat("NUM_ROWS is", NUM_ROWS) } df_pr(t2) df_pr(pr,t2) ?nrow nrow(t2) ?vector vector(mode="numeric",length=3) # full data-frame prediction... df_pr <- function(FUN,df) { NUM_ROWS <- nrow(df) cat("NUM_ROWS is", NUM_ROWS) predictions = vector(mode="numeric",length=NUM_ROWS) for (row in 1:NUM_ROWS) { next_prediction = pr(df[row,]) predictions[row] = next_prediction } return(predictions) } df_pr(pr,t2) # full data-frame prediction... df_pr <- function(FUN,df) { NUM_ROWS <- nrow(df) #cat("NUM_ROWS is", NUM_ROWS) predictions = vector(mode="numeric",length=NUM_ROWS) for (row in 1:NUM_ROWS) { next_prediction = pr(df[row,]) predictions[row] = next_prediction } return(predictions) } x <- df_pr(pr,t2) x t2 x t2[1] t2[[1]] x - t2[[1]] sum(as.numeric(x == t2[[1]])) sum(x == t2[[1]]) y <- x y[2] y[2] <- 0 y x t2[t2$survived!=y] t2 t2$survived t2$survived!=y t2[t2$survived!=y,] t <- read.csv("train742.csv") View(t) View(t) ?scan mhall <- function() { cat("Enter a number:") user = scan(n=1) cat("You entered",user) } mhall() ?runif ?sample sample(seq(3),1) replicate( 10, sample(seq(3),1)) save.image("C:/Users/Owner/Desktop/wksp1.Rwk.RData") sample(seq(3),1) replicate( 10, sample(seq(3),1)) } source('C:/Users/Owner/Desktop/titanic.R', echo=TRUE) source('C:/Users/Owner/Desktop/titanic.R', echo=TRUE) pr0(42) t2 source('C:/Users/Owner/Desktop/titanic.R', echo=TRUE) t2 pr_df(pr0,t2) sum(t2[t2$survived == pr_df(pr0,t2)]) t2$survived t2$survived == pr_df(pr0,t2) sum(t2$survived == pr_df(pr0,t2)) print("Hi") x <- 42 print("Hi", x) cat("Hi",x) ?cat cat("Hi",x) View(sp) sp ?read.csv s <- read.csv("states.csv") View(s) View(sp) View(s) summary(s) hist(sp) sp sp <- s$pop mode(sp) hist(sp) sp <- as.numeric(sp) hist(sp) sp <- s$Pop sp mode(sp) hist(sp) hist(sp,breaks=20) ?quantile quantile( sp, c(.5,.95)) quantile( sp, c(.05,.95)) mean(sp) sd(sp) 1:10 sample( 1:10, 3 ) sample( sp, 16 ) samp <- sample( sp, 16 ) mean(samp) man(sp) mean(sp) samp <- sample( sp, 16 ) ; mean(samp) samp <- sample( sp, 16 ) ; mean(samp) samp <- sample( sp, 16 ) ; mean(samp) samp <- sample( sp, 16 ) ; mean(samp) samp <- sample( sp, 16 ) ; mean(samp) 42 ; 43 sampdist <- replicate( 1000 , mean(sample( sp, 16 )) ) length(sampdist) summary(sampdist) mean(sp) hist(sampdist) sampdist <- replicate( 10000 , mean(sample( sp, 16 )) ) hist(sampdist) unknown_mean = 4000000 quantiles( sampdist, c(.05,.95)) quantile( sampdist, c(.05,.95)) mean(sp) median(sp) sort(sp) sorted <- sort(sp) sorted[26] ?1which ?1which ?which which(s$Pop==sorted[26]) s[18] s[18,] s[s$Pop == sorted[26]] s[s$Pop == sorted[26],] s[s$Pop == sort(sp)[length(sp)/2],] length(sp)/2 sort(sp)[25.5] sort(sp)[25] s[s$Pop == sort(sp)[(length(sp)+1)/2],] which( c(1,2,3,2)==2 ) which( c(7,8,9,8)==2 ) which( c(7,8,9,8)==8 ) s[which( c(7,8,9,8)==8 ),] savehistory("C:/Users/Owner/Desktop/median_example.txt")