You are here: Vanderbilt Biostatistics Wiki>Main Web>StatComp>RS>RProgramming (revision 2)EditAttach

R Programming | ProgrammingTipsForStatisticians

Handouts and Online Books

Common R Programming Techniques That Can Be Improved

Attaching Data Frames

Attaching a data frame makes it easy to reference its variables, but if you have two data frames attached things can get confusing, and users frequently forget to detach an attached object. Better to use data=, (when calling functions that use the statistical modeling language), with or within. within is an alternative to transform and upData. Unlike with, within allows you to change or add variables in the referenced data frame if it was a storable object (e.g., not subscripted).

xyplot(y ~ x | g, data=mine)
   plot(x, y)
   plot(x, z)
   y <- 2*y
   x <- x-1
   new <- x + y

Logical Operations

R can subscript using integer vectors consisting of those subscripts meeting a certain condition, or using logical TRUE/FALSE vectors whose lengths are the lengths of the original objects tested. The latter usually leads to more readable and reliable code. Instead of

male <- which(sex=='male')
al <- which(state=='AL')
mean(x[intersect(male,al)])  # mean of male Alabamians

mean(x[sex=='male' & state=='AL'])  #or:
maleal <- sex=='male' & state=='AL'

Repetitive Statistical Analyses

Instead of

spearman2(y ~ x1 + x2, data=mydata, subset=sex=='male')
spearman2(y ~ x1 + x2, data=mydata, subset=sex=='female')

for(sx in levels(mydata$sex))
   cat('\n------------------------------\n', sx, '\n\n')
   s <- spearman2(y ~ x1 + x2, data=mydata, subset=sex==sx)
Edit | Attach | Print version | History: r4 < r3 < r2 < r1 | Backlinks | View wiki text | Edit WikiText | More topic actions...
Topic revision: r2 - 30 Mar 2009, FrankHarrell

This site is powered by FoswikiCopyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback