# Today I Learned: ____

#### Dason

Yeah but it looks weird. I'd rather it just be the short message which is why I know I add the [noquote]$$\hspace{1cm}$$[/noquote] stuff instead of using that other hack.

#### ted00

##### New Member
Code:
> cv = c("a","b","c")
> do.call("paste",c(as.list(cv),sep=", "))
[1] "a, b, c"
A simple one, yes, but it took me few minutes to get it ... concatenation of vector with separator

#### Dason

Was this just a trial to figure what was going on or... were you not aware of the "collapse" parameter?

#### TheEcologist

##### Global Moderator
Today I learned more about closures.

Code:
 foo <- function(x) { bar <- function(y) { return(x * y) }; return(bar) }
Then try:
Code:
foo(33)(11)

## [1] 363

#### Dason

oh now you've done it. Mention the word closures and BG is going to get so excited he'll need a new pair of pants.

#### jk90029

##### Member
Did you know manipulate() ?

Dear-

This is not a question, but recently found a useful function in RStudio.

install.packages("manipulate")
library(manipulate)

manipulate(
+ plot(c(1,3,2), type = "l",
+ ylim=c(y.min,y.max)),
+ y.min = slider(0,5),
+ y.max = slider(2,10, initial = 2))

makes one to allow a change the length of interest by himself , after drawing a graph.

#### hlsmith

##### Less is more. Stay pure. Stay poor.
Re: Did you know manipulate() ?

Sound like a power package I saw once that showed the trade offset between type I and II errors.

#### Dason

It's covered in the documentation but I didn't realize this before... You can use length to change the length of a vector.

Code:
> x <- 1:100
> x
[1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
[19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
[37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
[55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
[73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
[91]  91  92  93  94  95  96  97  98  99 100
> length(x) <- 3
> x
[1] 1 2 3

#### bryangoodrich

##### Probably A Mammal
Interesting. I wonder if that could in any way help with space saving (in some loop, say), such as allocating size to a vector (fills with NAs). The advantage is probably marginal, though. I'm also a pessimist!

#### trinker

##### ggplot2orBust
Today I learned how to average a list of matrices by turning it into an array and then rowmeans:

Code:
[COLOR="silver"]## Create a list of similar matrices[/COLOR]
mat <- do.call(rbind, lapply(1:10, function(i) 1:10))
list_o_matrices <- lapply(1:15, function(i) jitter(mat))

[COLOR="silver"]## Average them[/COLOR]
ave_matrix <- rowMeans(simplify2array(list_o_matrices), dim = 2)

ave_matrix

[COLOR="silver"]##            [,1]     [,2]     [,3]     [,4]     [,5]     [,6]     [,7]     [,8]     [,9]     [,10]
##  [1,] 1.0014703 2.011833 3.044069 3.967024 5.006964 5.971793 6.925115 7.990019 9.021979 10.015695
##  [2,] 1.0246623 2.019152 3.004829 3.957002 4.981350 6.028579 7.038584 8.007656 8.955430  9.973690
##  [3,] 1.0555988 1.989264 2.963845 4.027272 5.026608 6.036112 7.031711 7.958438 8.989878 10.025474
##  [4,] 1.0518842 1.983410 3.029770 4.018368 4.990986 6.057223 6.997357 7.979873 8.922734  9.986551
##  [5,] 0.9832327 2.024032 2.969966 4.015551 4.997545 5.981654 6.992704 8.039162 8.982212 10.070446
##  [6,] 0.9531061 2.028616 3.014846 4.015529 4.970212 6.019053 6.957198 8.013462 8.994654 10.015937
##  [7,] 0.9967407 1.974352 2.990255 3.987084 4.987525 5.973495 7.080626 7.977638 8.978614  9.999084
##  [8,] 1.0342885 1.957007 3.007492 4.047000 5.017924 5.987988 7.074926 7.931602 8.982353  9.974031
##  [9,] 1.0120165 1.981054 3.038811 4.060915 4.988357 6.038697 7.023861 7.996769 9.005903  9.967318
## [10,] 0.9792814 1.968815 3.004207 4.014563 5.013227 5.952917 6.956201 7.969725 8.991927 10.047069[/COLOR]

#### Dason

I'm not saying it's better but my train of thought went to using apply with MARGIN=c(1,2) and FUN=mean. Your way is probably faster.

#### verystrongjoe

##### New Member
TIL : Import a r file in r file can be possible. use source() method

#### trinker

##### ggplot2orBust
Environments modify in place and don't make copies, making it an efficient way to store data within a function that has many steps. You can pas the environment to child/helper functions, modify it but no need to return the environment as it was modified in the original location:

Code:
f1 <- function(x, env) {
env$X <- x TRUE } f2 <- function(x, env) { env$Y <- x
TRUE
}

f3 <- function(x, env) {
env\$Y * x
}

f <- function(x){
e <- new.env()
f1(1, e)
f2(2, e)
out <- f3(x, e)
return(list(out, e))
}

out <- f(10)
out

ls(out[[2]])
Yields:

Code:
## > ls(out[[2]])
## [1] "X" "Y"
## > out
## [[1]]
## [1] 20
##
## [[2]]
## <environment: 0x000000001c4f0820>
##
## >
## > ls(out[[2]])
## [1] "X" "Y"
In short...altering the environment e in functions f1 & f2 actually changed environment e in function f

#### DV Man

##### New Member
When you copy and paste a file into Excel for use in R, make sure you use the text import wizard and align the columns so each line can be fully read:shakehead

#### DV Man

##### New Member
I learned that "stat = hline" was removed from ggplot2 2.0, but "geom_line(stat = 'summary', fun.y = "mean", lty = 3) will work just fine.

#### Junes

##### Member
Today I found out about eval(), which evaluates a chunk of R code. I used it to be able to do this:

Code:
plotMargins <- function(fac) partialPlot(model, dataset, eval(fac))
margins <- lapply(c("factor1", "factor5", "factor9"), plotMargins)
partialPlot is from the Random Forest package and the third parameter is a variable for which the partial dependence plot is to be calculated. Running it without eval() results in an error because the function doesn't accept a character. However, eval() "turns the character into R code" and it works.

I've seen weird situations like this before. Good to know how to fix it.