收藏 分享(赏)

r - variables, data frames etc.pdf

上传人:weiwoduzun 文档编号:1753662 上传时间:2018-08-22 格式:PDF 页数:26 大小:122.22KB
下载 相关 举报
r - variables, data frames etc.pdf_第1页
第1页 / 共26页
r - variables, data frames etc.pdf_第2页
第2页 / 共26页
r - variables, data frames etc.pdf_第3页
第3页 / 共26页
r - variables, data frames etc.pdf_第4页
第4页 / 共26页
r - variables, data frames etc.pdf_第5页
第5页 / 共26页
点击查看更多>>
资源描述

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 经营企划

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报