Set Up
I have a data.frame
full of some junk:
> set.seed(12345)
> d <- data.frame(x=rnorm(100), y=rnorm(100), z=rnorm(100)) %>% tbl_df
> d %>% head(3)
Source: local data frame [6 x 3]
x y z
1 0.5855288 0.2239254 -1.4361457
2 0.7094660 -1.1562233 -0.6292596
3 -0.1093033 0.4224185 0.2435218
Now I want to look at some relationships between my variables:
> '%cor%' <- function(x,y) round(100 * cor(x,y, use='pairwise'), 1)
> d %>% summarize(x %cor% y, y %cor% z, lag(x) %cor% z)
Source: local data frame [1 x 3]
x %cor% y y %cor% z lag(x) %cor% z
1 10.4 -12.7 7.8
The Problem
The names of my columns are larger than the data itself.
Bad Solution #1: Manual Names
One thing I can do is name the columns myself, but this requires additional book-keeping so it gets annoying quickly:
> d %>% summarize(x2y=x %cor% y, y2z=y %cor% z, Lx2y=lag(x) %cor% z)
Source: local data frame [1 x 3]
x2y y2z Lx2y
1 10.4 -12.7 7.8
Bad Solution #2: Transpose
Another thing I can do is transpose:
> d %>% summarize(x %cor% y, y %cor% z, lag(x) %cor% z) %>% t
[,1]
x %cor% y 10.4
y %cor% z -12.7
lag(x) %cor% z 7.8
The problem is that when I start grouping, it down-casts the columns:
> d %>% mutate(group=cut(z, c(-Inf,0,Inf))) %>%
group_by(group) %>%
summarize(x %cor% y, y %cor% z, lag(x) %cor% z) %>% t
[,1] [,2]
group "(-Inf,0]" "(0, Inf]"
x %cor% y " 6.9" "14.9"
y %cor% z "-19.8" "-17.3"
lag(x) %cor% z " 3.9" "-6.3"
Is there a way to tell print.data.frame(...)
that I want it to display series horizontally instead of vertically?
Bad Solution #3: grid.table
The other thing I've tried is rendering using gridExtra::grid.table
which helps, but it would be nice if I could rotate the column names vertically:
> gt <- function(df) {
grid.newpage()
df %>% grid.table(core.just='right', show.rownames=F)
}
> d %>% summarize(x %cor% y, y %cor% z, lag(x) %cor% z) %>% gt
Is there any way to do that?