1

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

enter image description here

Is there any way to do that?

dvmlls
  • 2,206
  • 2
  • 20
  • 34

2 Answers2

1

There are several possible solutions to your problem. For each suggestion you make in your question, I have listed a way to complete the idea. Personally I think the transposing solution (#2) is the best one.

Solution #1: Manual Names

Instead of really manual names, you can encode your preferred names in a function (e.g. naming_convention). Then you call this function to rename the entire data.frame.

# use this function to define all naming you would like to use
naming_convention <- function(data){
  colnames(data) <- gsub(' %cor% ', '2', colnames(data)) # rename %cor% with 2
  colnames(data) <- gsub('lag', 'L', colnames(data))     # rename lag with L
  colnames(data) <- gsub(' |[(]|)', '', colnames(data))  # remove () 
  return(data)
}
# call function
d %>% summarize(x %cor% y, y %cor% z, lag(x) %cor% z) %>%
  naming_convention
## Source: local data frame [1 x 3]
## 
##    x2y   y2z Lx2z
## 1 10.4 -12.7  7.8

Solution #2: Transpose

Convert the matrix back into a data.frame for proper printing.

d %>% mutate(group=cut(z, c(-Inf,0,Inf))) %>% 
  group_by(group) %>% 
  summarize(x %cor% y, y %cor% z, lag(x) %cor% z)%>% 
  t %>% 
  data.frame
##                      X1       X2
## 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

Solution #3: grid.table

This is probably not such a good idea, but you can use the previous two strategies together with a call to grid.table.

# manual rename
gt1 <- function(df) { 
  grid.newpage()
  nm <- gsub(' %cor% ', '2', colnames(df)) # rename %cor% with 2
  nm <- gsub('lag', 'L', nm)     # rename lag with L
  nm <- gsub(' |[(]|)', '', nm)  # remove () 
  df %>% grid.table(core.just='right', theme=list(cols=nm))
}
d %>% mutate(group=cut(z, c(-Inf,0,Inf))) %>% 
  group_by(group) %>% 
  summarize(x %cor% y, y %cor% z, lag(x) %cor% z)%>% 
  gt1
# transpose
gt2 <- function(df) { 
  grid.newpage()
  df %>% t %>% grid.table(core.just='right')
}
d %>% mutate(group=cut(z, c(-Inf,0,Inf))) %>% 
  group_by(group) %>% 
  summarize(x %cor% y, y %cor% z, lag(x) %cor% z)%>% 
  gt2
shadow
  • 21,823
  • 4
  • 63
  • 77
0
summarized.correlation = function(d) {
    correlations = cor(d) * 100
    s.correlations = correlations[upper.tri(correlations)]

    namez = apply(combn(colnames(correlations), 2), 2, function(pairs) { do.call(function(...) { paste(..., sep="2")}, as.list(pairs)) })
    names(s.correlations) = namez
    s.correlations
}
Andrew Cassidy
  • 2,940
  • 1
  • 22
  • 46