##1 Data manipulation
-
If the data contains NA values, it regards it as factor, not numeric.
DATA$COLUMN <- as.numeric(as.character(DATA$COLUMN)) -
Rename the column:
names(DATA)[2] <- "NEW_NAME" -
Melt the data for ggplot2 plotting
df <- melt(DATA, id.vars="COLUMN", na.rm=T) -
Remove NA in melt:
na.rm = T -
Remove NA from a dataframe
df <- na.omit(df) -
Change the digits of data
format(DATA$COLUMN, digits=4, scientific=F) -
Change the dimnames of a matrix
dimnames = list(c("O", "C", "L", "H"), c("O", "C", "L", "H")) -
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 -
Add the ratio of two specific colomn to a data frame
ratio <- transform(DATA, ratio = paste(round(COLUMN1 / COLUMN2*100, digits=2), "%") -
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
-
Change the labels of xlab
scale_x_date(labels = date_format("%m-%Y")) #need timeDate package -
Omit the title of xlab
xlab("") -
Change the color to grey scale
scale_color_grey() -
Change the position of legend
theme(legend.position="bottom") -
Annotate the plot
myvalue <- format(DATA$COL, digits=4, scientific=F) #Set the tag geom_text(aes(Var1, Var2, label=myvalue), color="snow") -
Change the font size of axes
theme(axis.text.x=element_text(size=15), axis.text.y=element_text(size=15)) -
Remove the legend
theme(legend.position="none") -
Change the legend label and change the color to gray scale
scale_fill_grey(labels=c("A", "B") -
Print a high-res png file
ggsave(plot=last_plot(), file="FILENAME.png", dpi=300) -
Set the limit of axix
scale_y_continous(limits=(0, 200)) xlim(0,30)
##3 ACF and CCF test
-
Manipulate the data to a matrix
m.DATA <- as.matrix(DATA[2:5]) -
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 -
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 -
Change the label of the axis in heatmap of ACF result
DATA$COLUMN[DATA$COLUMN == 1] <- "Day0"
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= "+")))