Skip to content

Instantly share code, notes, and snippets.

@illy
Last active December 14, 2015 08:28
Show Gist options
  • Save illy/5057694 to your computer and use it in GitHub Desktop.
Save illy/5057694 to your computer and use it in GitHub Desktop.
Some useful R commands.

##1 Data manipulation

  1. If the data contains NA values, it regards it as factor, not numeric.

     DATA$COLUMN <- as.numeric(as.character(DATA$COLUMN))
    
  2. Rename the column:

     names(DATA)[2] <- "NEW_NAME"
    
  3. Melt the data for ggplot2 plotting

     df <- melt(DATA, id.vars="COLUMN", na.rm=T)
    
  4. Remove NA in melt:

     na.rm = T
    
  5. Remove NA from a dataframe

     df <- na.omit(df)
    
  6. Change the digits of data

     format(DATA$COLUMN, digits=4, scientific=F)
    
  7. Change the dimnames of a matrix

     dimnames = list(c("O", "C", "L", "H"), c("O", "C", "L", "H"))
    
  8. Extract the specific area of a data frame

     DATA2 <- DATA[1:(nrow(DATA)-1), 3:5] 
    
     #extract the value from 1st row to (nrow-1), 3rd-5th columns
    
  9. Add the ratio of two specific colomn to a data frame

     ratio <- transform(DATA, ratio = paste(round(COLUMN1 / COLUMN2*100, digits=2), "%")
    
  10. Remove rows from a dataframe according to specific conditions

    a <- a[which(a$COL1!= "2012-03-24" & a$COL1!= "2012-03-25"] #diffrent rows
    
    or
    
    a <- a[!(a$COL2=="A" & a$COL3=="B"),] #one row with different conditions
    

##2 ggplot2

  1. Change the labels of xlab

     scale_x_date(labels = date_format("%m-%Y")) #need timeDate package
    
  2. Omit the title of xlab

     xlab("")
    
  3. Change the color to grey scale

     scale_color_grey()
    
  4. Change the position of legend

     theme(legend.position="bottom")
    
  5. Annotate the plot

     myvalue <- format(DATA$COL, digits=4, scientific=F) #Set the tag
    
     geom_text(aes(Var1, Var2, label=myvalue), color="snow")
    
  6. Change the font size of axes

     theme(axis.text.x=element_text(size=15), axis.text.y=element_text(size=15))
    
  7. Remove the legend

     theme(legend.position="none")
    
  8. Change the legend label and change the color to gray scale

     scale_fill_grey(labels=c("A", "B")
    
  9. Print a high-res png file

     ggsave(plot=last_plot(), file="FILENAME.png", dpi=300)
    
  10. Set the limit of axix

    scale_y_continous(limits=(0, 200))
    
    xlim(0,30)
    

##3 ACF and CCF test

  1. Manipulate the data to a matrix

     m.DATA <- as.matrix(DATA[2:5])
    
  2. Apply the ACF test

     acf.DATA <- acf(DATA, na.action=na.contiguous, plot = F) 
     
     #find the longest contiguous stretch of non-NAs
     #turn off the plot
    
  3. Extract the ACF matrix from the ACF result

     m.acf.DATA <- matrix(acf.DATA$acf, ncol = 4, byrow = T)
    
     #make a matrix according to the result
    
  4. Change the label of the axis in heatmap of ACF result

     DATA$COLUMN[DATA$COLUMN == 1] <- "Day0"
    

Machine learning

rpart(y~., data)
rpart(y~.-V1, data)

library(e1071) 
svm(x=xMatrix, y=yVector, ...) 


xnam <- paste("x", c(1,3,9:20,50), sep="") 
fmla <- as.formula(paste("y ~ ", paste(xnam, collapse= "+")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment