Skip to content

Commit

Permalink
Write section on with/within
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbarton committed Apr 1, 2012
1 parent 95dabc5 commit 1affb2b
Showing 1 changed file with 78 additions and 1 deletion.
79 changes: 78 additions & 1 deletion content/markup/software/longform/r-the-beautiful-parts.markdown
Expand Up @@ -161,7 +161,84 @@ I think this serves as an example that if you follow the R way of doing things
then your code is compact and simple. The alternative is to end up fighting then your code is compact and simple. The alternative is to end up fighting
against R to get the results you need. against R to get the results you need.


### with/within ### Prefer with/within over attach

Accessing each each column vector from a data fame using the `$` name prefix is
cumbersome. The `attach` method can be used to get around this by directly
adding the column vectors into the R search path. Using this method does not
however add any changes made to these variable back into the parent data frame.
For example:

my.data <- data.frame(x = 1:3,
y = (1:3)*2 + 2)
my.data
#=> x y
# 1 1 4
# 2 2 6
# 3 3 8

# Calculate the x*y product
# Using the `$` verbose notation
my.data$z <- my.data$x * my.data$y
my.data
#=> x y z
# 1 1 4 4
# 2 2 6 12
# 3 3 8 24

# Calculate using attach
# The x and y variables can be directly accessed
attach(my.data)
z <- x * y

# The new variable `z` is however not part of my.data
my.data
#=> x y
# 1 1 4
# 2 2 6
# 3 3 8

The `with` and `within` functions allow you to do many of the functions you
might need to do with a data without verbose syntax whilst still maintaining
the data as part of the data frame. For example the following highlights using
`within` to perform the same operations:

# Using `within` returns the updated to the data frame
my.data <- within(my.data, z <- x * y)
my.data
#=> x y z
# 1 1 4 4
# 2 2 6 12
# 3 3 8 24

# Multiple, multi-line operations using curly brace expressions
my.data <- within(my.data,{
z1 <- x * y
z2 <- x + y
})
my.data
#=> x y z1 z2
# 1 1 4 4 5
# 2 2 6 12 8
# 3 3 8 24 11

The with function is similar to the within function where instead the result of
the expression is returned rather than updating the passed data frame. This
useful for creating models or plots from data frames, where the last call is
returned. This can be illustrated as follows:

# The result of the expression is returned rather
# than updating the data
with(my.data, z <- x * y)
#=> 4 12 24

# Create a linear model
with(my.data, {
z <- x^2 + 1
lm(y ~ z)
})
#=> The linear model is returned

### apply ### apply
### anonymous functions ### anonymous functions
### conditionals if/switch ### conditionals if/switch
Expand Down

0 comments on commit 1affb2b

Please sign in to comment.