Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Write section on with/within

  • Loading branch information...
commit 1affb2b472e9d34379aec430e281f1747268cfb7 1 parent 95dabc5
Michael Barton authored
79 content/markup/software/longform/r-the-beautiful-parts.markdown
Source Rendered
@@ -161,7 +161,84 @@ I think this serves as an example that if you follow the R way of doing things
161 161 then your code is compact and simple. The alternative is to end up fighting
162 162 against R to get the results you need.
163 163
164   -### with/within
  164 +### Prefer with/within over attach
  165 +
  166 +Accessing each each column vector from a data fame using the `$` name prefix is
  167 +cumbersome. The `attach` method can be used to get around this by directly
  168 +adding the column vectors into the R search path. Using this method does not
  169 +however add any changes made to these variable back into the parent data frame.
  170 +For example:
  171 +
  172 + my.data <- data.frame(x = 1:3,
  173 + y = (1:3)*2 + 2)
  174 + my.data
  175 + #=> x y
  176 + # 1 1 4
  177 + # 2 2 6
  178 + # 3 3 8
  179 +
  180 + # Calculate the x*y product
  181 + # Using the `$` verbose notation
  182 + my.data$z <- my.data$x * my.data$y
  183 + my.data
  184 + #=> x y z
  185 + # 1 1 4 4
  186 + # 2 2 6 12
  187 + # 3 3 8 24
  188 +
  189 + # Calculate using attach
  190 + # The x and y variables can be directly accessed
  191 + attach(my.data)
  192 + z <- x * y
  193 +
  194 + # The new variable `z` is however not part of my.data
  195 + my.data
  196 + #=> x y
  197 + # 1 1 4
  198 + # 2 2 6
  199 + # 3 3 8
  200 +
  201 +The `with` and `within` functions allow you to do many of the functions you
  202 +might need to do with a data without verbose syntax whilst still maintaining
  203 +the data as part of the data frame. For example the following highlights using
  204 +`within` to perform the same operations:
  205 +
  206 + # Using `within` returns the updated to the data frame
  207 + my.data <- within(my.data, z <- x * y)
  208 + my.data
  209 + #=> x y z
  210 + # 1 1 4 4
  211 + # 2 2 6 12
  212 + # 3 3 8 24
  213 +
  214 + # Multiple, multi-line operations using curly brace expressions
  215 + my.data <- within(my.data,{
  216 + z1 <- x * y
  217 + z2 <- x + y
  218 + })
  219 + my.data
  220 + #=> x y z1 z2
  221 + # 1 1 4 4 5
  222 + # 2 2 6 12 8
  223 + # 3 3 8 24 11
  224 +
  225 +The with function is similar to the within function where instead the result of
  226 +the expression is returned rather than updating the passed data frame. This
  227 +useful for creating models or plots from data frames, where the last call is
  228 +returned. This can be illustrated as follows:
  229 +
  230 + # The result of the expression is returned rather
  231 + # than updating the data
  232 + with(my.data, z <- x * y)
  233 + #=> 4 12 24
  234 +
  235 + # Create a linear model
  236 + with(my.data, {
  237 + z <- x^2 + 1
  238 + lm(y ~ z)
  239 + })
  240 + #=> The linear model is returned
  241 +
165 242 ### apply
166 243 ### anonymous functions
167 244 ### conditionals if/switch

0 comments on commit 1affb2b

Please sign in to comment.
Something went wrong with that request. Please try again.