with within and transform in R I found…


mensagens de consolo para termino de namoro with, within, and transform in R.
I found that they are useful to deal with data set.
I don’t quite understand the explanation in the help page and I can’t not guarantee the correctness of my explanation so I provide some examples to show their behavior.
Fist of all, they need data frame or list but not matrix. These are three examples to add a column which is the sum of the first two columns. See the differences among the functions.

  • with: returns one column
  • within: returns the whole data
  • transform: returns the whole data but the function argument is slightly different.

Dhorāji with

testwith <- data.frame(x1 = 1:10, x2 = 11:20) > testwith$y <- with(testwith, {x1 + x2}) > testwith x1 x2 y 1 1 11 12 2 2 12 14 3 3 13 16 4 4 14 18 5 5 15 20 6 6 16 22 7 7 17 24 8 8 18 26 9 9 19 28 10 10 20 30

within

> testwith <- data.frame(x1 = 1:10, x2 = 11:20)
> testwith <- within(testwith, {y <- x1 + x2})
> testwith
   x1 x2  y
1   1 11 12
2   2 12 14
3   3 13 16
4   4 14 18
5   5 15 20
6   6 16 22
7   7 17 24
8   8 18 26
9   9 19 28
10 10 20 30

transform

> testwith <- data.frame(x1 = 1:10, x2 = 11:20)
> testwith <- transform(testwith, y = x1 + x2)
> testwith
   x1 x2  y
1   1 11 12
2   2 12 14
3   3 13 16
4   4 14 18
5   5 15 20
6   6 16 22
7   7 17 24
8   8 18 26
9   9 19 28
10 10 20 30

Some more examples at
http://stackoverflow.com/questions/1310247/in-r-do-you-use-attach-or-call-variables-by-name-or-slicing


Leave a Reply

Your email address will not be published. Required fields are marked *