10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Appendix E 388String$defineMethod("split".function(by="") {unlist(strsplit(string,by))})setMethod("/","String",function(e1,e2) el$split(e2))Again strsplit () does the hard work. We defined a new method, split, but didn’t have toworry about matching the <strong>for</strong>mal arguments of the previously defined split () function (anS3 generic method). However, when we use the S4 style to use the "/" syntax, we needour function to match the <strong>for</strong>mal arguments it uses (e1,e2).We can now “divide” our strings as follows:> flanders=String$new("Ned, Maude, Todd, Rod")> flanders$split() # into character bydefault[1] "N" "e" "d" "," " " "M" "a" "u" "d" "e" "," " ""T" "o" "d"[16] "d" "," " " "R" "o" "d"> flanders/" "[1] "Ned," "Maude," "Todd," "Rod"As mentioned, when using OOP we can modify the object in place. For example, wemight want to make the string uppercase. This can be done by defining an up casemethod as follows:String$defineMethod("upcase",function() {set.string(toupper(string))})Applying the function gives> flanders$upcase()> flandersNED, MAUDE, TODD, RODThe upcase () method uses both the string variable and the set. str ing () function, whichare created from the field names. Simple assignment to string will not work, although the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!