Today I Learned: ____

bryangoodrich

Probably A Mammal
#21
Yeah, don't copy the whole **** thing! It depends on where you start grabbing. The HTML is literally a sequence of numbers and then the code. If you highlight too much to copy, you'll grab the sequence of numbers.
 

Dason

Ambassador to the humans
#23
It's not technically a function but an interval variable that every top level function stores their output to before returning it to the user.
 

trinker

ggplot2orBust
#24
This is so simple and how dummy coding works but I never thought about using mean to find proportions of something. Today Dason added to a post I gave to an R user about find proportions of a value and his code was much simpler and faster.

Code:
x <- rnorm(1000, 5, 1.75)
sum(x > 0)/length(x) #gives the proportion using binary operator
mean(x > 0)  #Dason way that also gives the proportion using binary operator

     test replications elapsed relative user.self sys.self user.child sys.child
2   dason       100000    3.46 1.362205      3.13     0.00         NA        NA
1 trinker       100000    2.54 1.000000      2.11     0.07         NA        NA
 

Dason

Ambassador to the humans
#25
This is so simple and how dummy coding works but I never thought about using mean to find proportions of something. Today Dason added to a post I gave to an R user about find proportions of a value and his code was much simpler and faster.
It is simpler to write and less code (so faster to write too) but interestingly enough as your output shows using mean actually is a performance hit. I wouldn't have guessed that.
 

jpkelley

TS Contributor
#27
Why hadn't I seen this before...

In lattice graphics, it's easy to save a plot as an object. I was previously unaware of something similar in base graphics (but I'm sure you all have). Anyway, a convenient little gem of a function. Use after making your plot as such...

Code:
plotname<-recordPlot()
 

jpkelley

TS Contributor
#28
Although I won't post code, I also learned today how to interpret (and use) model-averaging for Generalized Additive Models (package mgcv). Once I sat down with it, it was relatively simple. Allowed me to examine exactly which parts of my 3-D selection surfaces had a positive or negative effect (by examining confidence intervals on the link scale). Model-selection and model-averaging with GAM and GAMM is a bee-otch.

Anyway, just needed to tell someone about the small steps made today!
 

jpkelley

TS Contributor
#30
Ha...well said. I just don't like having to deal with more than one syntax for simple tasks. I like ggplot2, but I think I'm going to stay in the base world and build a better foundation there.

R.I.P. for now ggplot2.
 

Dason

Ambassador to the humans
#33
I use ggplot for convenience. But I'm mainly of TE's mindset - I should know how to do anything I want using base. I just save time using ggplot2.
 

trinker

ggplot2orBust
#34
I love base and ggplot for different reasons. They're different tools for different jobs. I do construction work around the house and have a nice array of tools. I just bought a new osculating tool that saws things off flush and used it last night to saw off some trim flush. I'm glad I have this tool. It's awesome and saves time. I won't be using it to pound in nails anytime soon though. I'm not an either or kinda guy. I'm a pragmatist, best tool for the job. I'm going to eventually learn lattice as well because I believe under certain circumstances this is the best tool for the job. Why limit yourself to one visualization technique?

I stand firm and tall; after all a velociraptor's gotta stand for something.


I'm calling out the TE.

Code:
library(data.table)
begin.time <- Sys.time()
require(ggplot2)
p <- ggplot(mtcars, aes(hp, mpg)) 
p + geom_point(aes(colour=as.factor(gear))) + facet_grid(cyl~gear, margins=T)
timetaken(begin.time)
View attachment 1716
Code:
> timetaken(begin.time)
[1] "00:01:55"
Challenge: gather the same visualization information in base in less than 1 min 55 sec
 

Dason

Ambassador to the humans
#35
It only took my computer about 4 seconds. Also you could do the timing without the "timetaken" function that I'm assuming is why you included data.table?

Code:
start <- Sys.time()
# code code
Sys.sleep(1)
end <- Sys.time()
end - start
 

Jake

Cookie Scientist
#36
I have spent longer than 1:55 looking at the plot and I'm still not completely sure what is going on. Maybe this is not such a good challenge.
 

trinker

ggplot2orBust
#37
DASON said:
It only took my computer about 4 seconds. Also you could do the timing without the "timetaken" function that I'm assuming is why you included data.table?
Yes the timer was to see how long it took me to write the code and plot it
 

trinker

ggplot2orBust
#38
Jake said:
I have spent longer than 1:55 looking at the plot and I'm still not completely sure what is going on. Maybe this is not such a good challenge.
Probably, partly because I made a mistake in using gear for color instead of carb

Jake I think a better way to understand the graph is to study the code:

Code:
require(ggplot2)
p <- ggplot(mtcars, aes(hp, mpg)) 
p + geom_point() #just a plot of hp and mpg
p + geom_point(aes(colour=as.factor(carb)))  # a plot colored by carb (I meant to do this in my plot above but used gear instead
p + geom_point(aes(colour=as.factor(carb))) + facet_grid(cyl~gear)  #facet plot of both cyl and gear
p + geom_point(aes(colour=as.factor(carb))) + facet_grid(cyl~gear, margins=T)   #same as before but with the margins
I'm not sure if you're a user of ggplot but the faceting I used is pretty familiar to ggplot users. For me it's one of the most useful aspects of ggplot.

The plot is a facet plot that works similarly to reshape. The graph is not publishable, but for me, allows me to quickly look at plots of data by group (and includes margins) and look for trends. If you're familiar with ggplot's faceting then the graph is pretty understandable, but again the plot is one I use frequently for initial data exploration. It's not polished but to do the same thing in base would take an exorbitant amount of time. As a researcher this tool has been invaluable.
 

bryangoodrich

Probably A Mammal
#40
In Python there is a nice way to index that would have obvious conflicts in R. This is the negative indexing. In Python, I can say something like someList[-2] to access the second to last element of the list, or to grab the last two elements I could say someList[-2:]. This is like sequence indexing in R on a vector: someVector[2:4]. In Python, though, the empty part of the sequence implies "to the end." In R, you have to be explicit

Code:
someList[2:]  # Python
someVector[2:length(someVector)] # R
In R, negatives are treated as "exclude" indicators. Thus, someVector[-2] would mean include all values except 2. In Python, as explained, it would grab that distance from the end of the vector.

What I learned today is that head has some nice properties that can mimic this Python behavior! I know that you can do something like

Code:
head(x, 20)  # Reveals 20 elements instead of default 5
What I did not realize is that the number can be a negative amount to exclude from the end.

Code:
head(x, -5)  # All but last 5 elements
Be creative. There are uses for this!