# Today I Learned: ____

#### Dason

Function seems broken to me. Here is the output I would have expected:
Code:
            person         person             person
name        "Trinker"      "Dason"            "bryangoodrich"
awesomeness "lacking"      [URL="http://xkcd.com/207/"]"A(g64,g64)"[/URL]       "mind blowing!"
profession  "velociraptor" "Raptor Hunter"    "Data Master"
status      "Evil"         "Happily Married"  "Single"

#### trinker

##### ggplot2orBust
I think you accidentally put a rev() around my vector

#### trinker

##### ggplot2orBust
One of my favorite functions "writeClipboard" only works for windows users. I always assumed everyone could use it. One of my favorite user defined functions wrap/unwrap (POST # 101 & 103 HERE) relies on writeClipboard.

I found out today via SO that writeClipboard can be done for Mac using:
Carl Witthoft said:
Code:
pipe("pbpaste") and pipe("pbcopy", "w")
I'd like to add this to the wrap unwrap functions but can't test it as I do not own a mac. I had envisoned something like

Code:
    if (Sys.info()[['sysname']] == "Windows") {
writeClipboard(noquote(zz), format = 1)
} else {
if(Sys.info()[['sysname']]=="OSX"){  #not sure what the Mac is called in R
pipe("pbcopy", "w")
}
}
I guess this is a negative today I learned. Sorry to be a Debbie Downer but as with most things in R "Where there's a will there's a way"

#### Dason

On my mac the sysname is "Darwin".
Code:
> j <- pipe("pbcopy", "w")
> cat("This is the text to put into the clipboard", file = j)
> close(j)
> This is the text to put into the clipboard # I just pasted that in
That's one way to write to the clipboard.

#### bryangoodrich

##### Probably A Mammal
I'm kind of disappointed in those apply examples. The author goes a very, very round about way of making aggregate work. This is, to me, an R work horse! The solution the author was aiming for was nothing more than:

Code:
aggregate(. ~ Species, iris, mean)
That's it! You don't need to split this frame or create subsets, etc. The formula version is the way to use aggregate, period! None of the other approaches beat this. Though, going that approach the author did, they could have used the aggregate(x, by, FUN) method with just

Code:
aggregate(subset(iris, select = -Species), by = list(Species = iris\$Species), mean)

#### trinker

##### ggplot2orBust
bryangoodrich said:
I'm kind of disappointed in those apply examples. The author goes a very, very round about way of making aggregate work. This is, to me, an R work horse!
Agreed bryangoodrich. I thought the same thing. Ever since you showed me the formula version of aggregate I've really used it a lot. But I did like the sqldf and data.table examples. It was nice to see all the different methods being shown at once.

#### bryangoodrich

##### Probably A Mammal
I agree. It's useful to see alternative ways to conceive of the solution. I especially like the apply function on the iris3 data array. It makes you think about how you might be able to solve your problem in a relatively easy approach if you manipulate your data into that format (or have it provided in that format for some reason). For instance, you can create a similar data set to iris3 from iris by doing the following.

Code:
x <- array(unlist(iris), dim = c(50, 3, 4), dimnames = list(NULL, c("Setosa", "Versicolor", "Virginica"), c("Sepal L.", "Sepal W.", "Petal L.", "Petal W.")))
apply(x, c(2, 3), mean)
apply(iris3, c(3, 2), mean) # To compare
The difference here is only in the ordering based on how the data was unlist from its original frame. I'm sure I could figure out another approach that more directly syncs it with iris3, but it is not worth the trouble at this time.

#### trinker

##### ggplot2orBust
TIL: What Reduce actually does. bryangoodrich was talking about how awesome it was a few weeks back but I was clueless. Today on R-bloggers markbulling posted a (LINK) to John Myles White's website that discusses higher order functions like Reduce in detail with simple examples. I've only worked through Reduce so far but there's other higher order functions in the Link to learn.

#### trinker

##### ggplot2orBust
TIL: Kinda of weird and may only be a windows or windows gui thing but...

If you open a data sets or history windows data() or history(n) you get windows that display information but aren't really interactive. I have at times used ctrl + c and ctrl + v to cut and paste the info from these windows to the R console. For example I might use ctrl + c [CO2] and the cntrl + v to cut and paste this data set to the console. By accident I highlighted CO2 today and the pressed cntrl + v and it sent the info to the console automatically as if I cut and pasted it. This works in history() but not in a regular script window. It's not the same as cntrl + r (in Rgui) because I still have to press enter to get the code to run.

From what I can tell this isn't a documented thing. it's only mildly useful but I thought it was still a hidden goodie that pleased me like finding an easter egg in a DVD.

#### bryangoodrich

##### Probably A Mammal
Nice link. I was looking into the Reduce and Map functions, as I heard they are common in other languages. In R, I noticed that Reduce does not provide anything in terms of an efficiency boost. Dason pointed that out to me onetime when a do.call proved better. I'm disappointed, though. The author didn't really explain Map at all! But since it is like the apply family of functions, I guess there is no point to it in R?

#### trinker

##### ggplot2orBust
bryangoodrich said:
The author didn't really explain Map at all! But since it is like the apply family of functions, I guess there is no point to it in R?
I think that was the sentiment of the LINK's author as well, especially considering lapply/apply are fairly easy to use and Map may slow you down. Reduce makes sense though.

#### bryangoodrich

##### Probably A Mammal
I also love the books the author references. If I do get a second degree in computer science, I'm probably gonna read those to improve my skillz!

The thing with Reduce is you need to know when to use it. Sure, it simplifies repeated tasks, but you could either do rbind multiple times or do do.call and feed it the list as an argument. Reduce will sequentially perform rbind on two elements at a time whereas the latter will rbind them all at once. It's the difference of

rbind(df1, df2, df3, df4) and rbind(rbind(rbind(df1, df2), df3), df4)

#### trinker

##### ggplot2orBust
I still need to figure out what do.call does. I'll get there eventually

#### bryangoodrich

##### Probably A Mammal
It's a way of saying "do this function and feed it these parameters as a list." Thus, if I say

Code:
do.call('rbind', as.list(df1, df2, df3))
I am effectively doing rbind(df1, df2, df3). So say I split some data by a factor and want to perform some function 'f' on each one. Then I can say

Code:
do.call('f', split(df, someFactor))
Of course, why not use by in that case? The point is that we have another avenue for invoking calls with a list of objects that would require a round-about way to achieve. You can also automate certain tasks by feeding it a function that may not be known beforehand (i.e., you pass it as a parameter). There are many uses for do.call when we go to another level of abstraction.

#### Lazar

##### Phineas Packard
TIL: unknownR package.......means I learnt a lot, though not all of it I needed or wanted to know

#### trinker

##### ggplot2orBust
TIRLFL (today I recalled long forgotten learning):

Sometimes when I index I use a sequence as in
Code:
mtcars[, 2:6]
you can't do the same with names though
Code:
mtcars[, cyl:wt] [COLOR="red"]#I'm bad and won't work :([/COLOR]
and at times it would be helpful {when I don't want to count or use which(cyl):which(wt)}
subset allows you to do this type of sequence indexing.
Code:
subset(mtcars, select=cyl:wt)

#### bryangoodrich

##### Probably A Mammal
If you recall, I had asked in the past if there's a way to grab zip file data over the internet. I mean, you can of course automate the download in R, read the file, and delete the download. I was hoping it could be done in one step like you make connections to other files. The problem is that a ZIP archive isn't really like a file, per se. It is like a directory. This is not true of .z, .gz, or .bz2 files, apparently. (Though, if I recall, I couldn't get a connection to work to those over the internet, either.)

Turns out I wasn't the only one curious about this. The good folks at SO provide a solution.

Code:
temp <- tempfile()
unlink(temp)
That's it. Clearly, this could be turned into a wrapper.

Code:
read.zip <- function(location, infile, ...) {
temp <- tempfile()
}  # end function
install.packages("RWeather")
getWeatherFromGoogle("50010", message = TRUE)