1、RVariables, data frames etc.R.M. RipleyDepartment of StatisticsUniversity of Oxford2008/9R.M. Ripley (University of Oxford) R 2008/9 1 / 19Using R as a calculatorComputer representation of numbersReal numbers are not stored exactly on computers. Use binaryversion of “scientific” notation e.g. 1:234
2、102This needs care e.g.x, =, =! (not), | (or), | (or), & (and) & (and)Note difference between |,& and |,&: former work on vectors,latter only consider one element.Exampleif x=(TRUE,FALSE,TRUE) and y=(FALSE,TRUE,TRUE) thenx|y=(TRUE,TRUE,TRUE), x|y=(TRUE),x&y=(FALSE,FALSE,TRUE), x&y=(FALSE)R.M. Ripley
3、 (University of Oxford) R 2008/9 13 / 19Manipulating variablesDates and TimesIf treated as character, easy to read, but not easy to manipulateTreated as special types, several different optionsSimple: as.Date() does not deal with timesPackage chron for dates and times but not time zonesPOSIXct and P
4、OSIXlt for times zones.All stored internally as numerics, but in different ways. Cansubtract two dates. Result is an object with class timediff.R.M. Ripley (University of Oxford) R 2008/9 14 / 19Manipulating variablesDates and Times: Examplesthe function as.Date() takes as arguments a character vari
5、ableand a format stringmyDate-as.Date(10-Jan-1993,format=%d-%b-%Y)class(myDate) is “Date“,as.numeric(myDate) is 8410, the number of days since 1stJanuary 1970.R.M. Ripley (University of Oxford) R 2008/9 15 / 19Manipulating variablesSome useful Functionsc,:,seq,repc(1,a) combines its arguments to for
6、m a vector. If any arecharacter (as here), the vector will be of characters.The colon (:) is useful for generating sequences:1:5 is the same as c(1,2,3,4,5)seq(1,10,by=2) gives (1,3,5,7,9)rep is also useful for creating vectors:rep(c(1,2),times=3) gives (1,2,1,2,1,2)rep(c(1,2),each=3) gives (1,1,1,2
7、,2,2)R.M. Ripley (University of Oxford) R 2008/9 16 / 19Manipulating variablesMore functionspastepaste adds together character vectors:paste(c(1,2),c(x,y,z) is a character vector oflength 31 “1 x“ “2 y“ “1 z“Notice the recycling of the first argument.The pieces of the result can be joined together u
8、sing theargument collapse:paste(c(1,2),c(x,y,z),collapse= ) is acharacter vector of length 1,“1 x 2 y 1 z“R.M. Ripley (University of Oxford) R 2008/9 17 / 19Manipulating variablesYet more functionssort,tablesort(x) sorts the vector x into increasing ordersort(x,decreasing=TRUE) sorts the vector x in
9、to decreasingordertable(x) creates a table showing the count of elements equal toeach value. Most useful where x is a vector of factors or integerse.g. table(rpois(20,5) gives2 3 4 5 6 7 83 2 2 6 3 3 1R.M. Ripley (University of Oxford) R 2008/9 18 / 19ExercisesExercises 2Create a list of all the dat
10、es in 2007, using seq and as.Date.There is a version of cut for dates, called cut.Date. Use this tocreate a factor corresponding to the week of the year in which thedates fall. Start the weeks on Sundays.Create a vector of length 100, with integer values in the range1 : 5, randomly ordered. (Hint: l
11、ook at the function sample.)Use paste to create a vector of labels: (Colour 1,Colour2,Colour 3,Colour 4,Colour 5)Use the factor command to create a factor from this vector, withthe labels created above.Create a data frame with 100 rows and two columns, one arandom sample of dates in 2007, and one the factor createdabove.R.M. Ripley (University of Oxford) R 2008/9 19 / 19