Month: October 2011

  • Difference using names or entries in a column…

    Difference using names or entries in a column selecting elements from data frame.
    Use names to select elements from data frame, if possible.
    When using the names, the slicing index does not need to the the same length as the data,
    but if one of the column is used, the index and the data should be the same length.

    Example

    test.df <- data.frame(cbind(letters[1:3], 1:3, 4:6)
    row.names(test.df) <- letters[1:3]
    
    # When the length of the index is different from the number of row of the data frame, it does not work.
    test.idx <- c('a', 'c')
    test.df[test.idx, ]
    test.df[test.df[[[1]], ]
    
    # When the length of the index is the same as the row of the data frame, it works
    test.idx <- c('a', 'b', 'c')
    test.df[test.idx, ]
    test.df[test.df[[[1]], ]
    
  • Different behaviors between data frame and matrix in…

    Different behaviors between data frame and matrix in R

    > # Generate an artificial matrix
    > test.m <- matrix(1:6, nrow = 3)
    > row.names(test.m) <- c('x1', 'x2', 'x3')
    > col.names(test.m) <- c('a', 'b')
    Error in col.names(test.m) <- c("a", "b") :
      could not find function "col.names<-"
      >
      > # Generate a data frame from the matrix
      > test.df <- as.data.frame(test.m)
      >
      >
      > # Selecting elements
      > ## the row names can be used to select elemnts from a data frame or a matrix
      > test.idx <- c('x3', 'x1')
      > test.df[test.idx, ]
         V1 V2
         x3  3  6
         x1  1  4
         > test.m[test.idx, ]
            [,1] [,2]
            x3    3    6
            x1    1    4
            >
            > # Selecting elements with index having a name which is not in the data
            > ## data frame returns NA rows
            > ## matrix returns an error
            > test.idx <- c('x4', 'x1')
            > test.df[test.idx, ]
               V1 V2
               NA NA NA
               x1  1  4
               > test.m[test.idx, ]
               Error: subscript out of bounds
               >
               >
               > # Duplicate row names
               > ## duplicate row names are not allowed in data frame.
               > ## duplicate row names are allowed in matrix.
               > test.row.names <- c('x1', 'x2', 'x1')
               > row.names(test.df) <- test.row.names
               Error in `row.names<-.data.frame`(`*tmp*`, value = c("x1", "x2", "x1")) :
                 duplicate 'row.names' are not allowed
                 In addition: Warning message:
                 non-unique value when setting 'row.names': ‘x1’
                 > rownames(test.m) <- test.row.names
                 >
                 > # names
                 > ## names() returns column name in data frame
                 > ## names() returns NULL in matrix
                 > names(test.df)
                 [1] "V1" "V2"
                 > names(test.m)
                 NULL
    
  • Using smooth spline in stat scale in ggplot2…

    Using smooth.spline in stat_scale in ggplot2

    smooth.spline2 <- function(formula, data, ...) { 
      mat <- model.frame(formula, data) 
      smooth.spline(mat[, 2], mat[, 1]) 
    } 
    
    predictdf.smooth.spline <- function(model, xseq, se, level) { 
      pred <- predict(model, xseq) 
      data.frame(x = xseq, y = pred$y) 
    } 
    
    qplot(mpg, wt, data = mtcars) + geom_smooth(method = "smooth.spline2", se= F)
    

    From: http://groups.google.com/group/ggplot2/browse_thread/thread/149dfa0891fe383a

  • Discussions about workflow using LaTeX PDF and textfile…

    Discussions about workflow using LaTeX, PDF, and textfile for collaborations with non-TeX users.

    http://tex.stackexchange.com/questions/1517/workflow-for-reviewing-pdfs-generated-from-tex
    http://tex.stackexchange.com/questions/16367/convert-tex-to-non-tex-and-back
    http://tex.stackexchange.com/questions/16367/convert-tex-to-non-tex-and-back/16406#16406

  • Lineno package which makes LaTeX and PDF reviews…

    Lineno package, which makes LaTeX and PDF reviews easier by printing line numbers.

    http://ctan.mackichan.com/macros/latex/contrib/ednotes/ulineno.pdf

  • Review PDF file with multi reviewers http crocodoc…

    Review PDF file with multi-reviewers.

    http://crocodoc.com/

  • synctex keys Ctrl click

    synctex keys

    Ctrl-click
    
  • Formatting text in Vim

    The gq{motion} command will format a section of text. The ip motion selects the current paragraph, so gqip applies formatting to the current paragraph. Running the gq command moves the cursor to the end of the paragraph. If you want to keep the cursor on the same word, you can instead run the command gw.

    http://vimcasts.org/episodes/hard-wrapping-text/

  • Moving easier around softwrapped text in VIM Put…

    Moving easier around softwrapped text in VIM.
    Put this in the .vimrc.

    "Show elipsis for softwrapped lines
    " Unicode for elipsis: ctrl-v v 2026
    set showbreak=…
    
    "Moving softwrapped lines
    vmap  gj
    vmap  gk
    vmap  g$
    vmap  g^
    vmap  g^
    nmap  gj
    nmap  gk
    nmap  g$
    nmap  g^
    nmap  g^
    

    Adapted from http://vimcasts.org/episodes/soft-wrapping-text/