Today I Learned: ____

Dason

Ambassador to the humans
#81
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
#83
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. :confused:

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

Ambassador to the humans
#84
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
#87
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
#88
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
#89
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
#90
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
#91
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
#92
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
#93
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
#94
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)
 

bryangoodrich

Probably A Mammal
#96
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.
 

trinker

ggplot2orBust
#98
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
#99
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()
download.file("http://www.newcl.org/data/zipfiles/a1.zip", temp)
data <- read.table(unz(temp, "a1.dat"))
unlink(temp)
That's it. Clearly, this could be turned into a wrapper.

Code:
read.zip <- function(location, infile, ...) {
    temp <- tempfile()
    download.file(location, temp)
    data <- read.table(unz(temp, infile, ...))
    unlink(temp)
}  # end function
I wonder if this works by specifying a URL or if using url may be necessary. It would also be useful to provide a way to handle the way the data is read in, because you may want to do read.csv or something. I haven't put too much thought into this, but since the process is just a few steps, the need for a convenience wrapper is not paramount. Still, it will prove useful because I keep running into data that people store in ZIP files!
 

Dason

Ambassador to the humans
Code:
install.packages("RWeather")
library(RWeather)
getWeatherFromGoogle("50010") # Check my weather FO REALZ
getWeatherFromGoogle("50010", message = TRUE)
TIL: I don't even need to look outside OR open a web browser to check the weather.