{ "contents" : "## FISH 552, Introduction to R\n## Beginning code for Lecture 6 -- Data Manipulation 2\n\n## This code is inteded to give you the datasets (made up or otherwise)\n# that we will use during the in-class examples. Add to this script the \n# code go over in class plus the Hands-On exercises. \n\n##########################\n#data used with apply and tapply\n#########################\n#Make a data matrix called subj\nrownm <- paste(\"Patient\",1:20)\ncolnm <- paste(\"Yr\",1:5)\nsubj <- matrix(round(rnorm(n=100),3),ncol=5,dimnames=list(rownm,colnm))\nhead(subj)\n\napply(subj, MARGIN=1, mean)\n\napply(subj, MARGIN=2, mean)\n\napply(subj, MARGIN=1, quantile, c(0.025, 0.975))\n\n\n#create 3 vectors that corespond to fish lengths, weights, and sex\nlengths <- sample(1:100,size=20, replace=T)\ngenders <- sample(c(\"Male\",\"Female\",\"Unknown\"), \n size=20, replace=T)\nweights <- sample(200:250, size=20, replace=T)\n\ntapply(X=lengths, INDEX=genders, FUN=mean)\ntapply(X=lengths, INDEX=list(genders, weights), FUN=mean)\n\n\n##################################\n#data used with order() and sort()\n###################################\n(cards <- sample(1:10)) #randomizes numbers from 1 to 10 and stores in object called cards\nsort(cards)\nrev(sort(cards))\norder(cards)\ncards[order(cards)]\nYY<-data.frame(ID=sample(1:10),dev=round(rnorm(10),3))\nYY[order(YY$ID),]\n# 10 normally distributed random numbers with mean 0 and std dev 1.\n# Rounded to 3 decimal places\n(rndNums <- round(rnorm(10),3))\nZZ<-data.frame(\nlaws <- sample(1:3,replace=T, size=10), #Randomly draw 10 numbers ranging from 1 to 3. Numbers can be used more than once.\nyear <- sample(2010:2012,replace=T,size=10), #Same as above but numbers range from 2010 to 2012\nstate <- c(\"WA\",\"OR\",\"CA\",\"VT\",\"NY\",\"RI\",\"FL\",\"UT\",\"AZ\",\"TX\") #10 different state abbriviations\n)\nZZ\nZZ[order(ZZ$laws, ZZ$year, ZZ$state),]\n##################################\n#Merge\n###################################\nstation1 <- cbind(time1=1:100, data=rnorm(100))\n\n#note: a common mistake when using seq is to get \n#the length of the resulting vector wrong. Here it is \n#length 21.\nstation2 <- cbind(time2=seq(0,100,5), \n category=sample(1:3,replace=T,size=21))\n\nstationX<-merge(station1, station2, by.x=\"time1\", by.y=\"time2\",all=T)\nintersect\n##################################\n#Working with dates and times\n###################################\n\neveryday <- seq(from=as.Date(\"2014-01-01\"), \n to=as.Date(\"2014-12-31\"), by=\"day\") \n#Note how you can use \"day\" as the interval in seq() when the data type is Date. Nifty.\n\n# loads a revised do.csv data file that has a column called dateTime that \n# contains the date and time for each data point in POSIX format.\ndo <- read.csv(\"do.csv\", header=T, colClasses=c(dateTime=\"POSIXlt\"))", "created" : 1413311472070.000, "dirty" : false, "encoding" : "UTF-8", "folds" : "", "hash" : "3447092038", "id" : "8444500F", "lastKnownWriteTime" : 1413319222, "path" : "~/R/IntroR/6_Lecture code.R", "project_path" : "6_Lecture code.R", "properties" : { }, "source_on_save" : false, "type" : "r_source" }