\cat programming
R is an environment for data analysis.
install.packages("package_name")
Function | Return |
nchar() | the number of characters in a string |
ncol() | the number of columns in a matrix |
nrow() | the number of rows in a matrix |
length() | the number of elements in an object |
lm()$coeff | the coefficients of linear regression |
zoom <- function(plotfunc, extent, final=FALSE){ # (C) 2007 GPL by Huidae Cho <http://geni.ath.cx> # Simple R function for interactive zooming/panning # # Example: # data <- runif(100)*10 # extent <- list(x=c(1, 100), y=c(0, 10)) # plotfunc <- function(lim){ # plot(data, xlim=lim$x, ylim=lim$y) # abline(mean(data), 0, col="red") # } # zoom(plotfunc, extent) if(!final){ cat(printf("Zoom in: Click two corners\n")) cat(printf("Zoom out: Click above plot\n")) cat(printf("Prev extent: Click left of plot\n")) cat(printf("Next extent: Click right of plot\n")) cat(printf("Full extent: Click below plot\n")) cat(printf("Pan: Double click\n")) cat(printf("Quit: Right button\n")) } lim <- extent lim.stack <- c(lim$x, lim$y) lim.depth <- 1 lim.cur <- 1 repeat{ plotfunc(lim) if(final) break l <- locator(1) if(is.null(l)) break ext <- par()$usr if(l$x < ext[1] || l$x > ext[2]){ cur <- lim.cur lim.cur <- if(l$x < ext[1]) max(lim.cur-1, 1) else min(lim.cur+1, lim.depth) if(lim.cur != cur) lim <- list(x=lim.stack[lim.cur, 1:2], y=lim.stack[lim.cur, 3:4]) next } if(l$y < ext[3]) lim <- extent else if(l$y > ext[4]){ cx <- (lim$x[1] + lim$x[2]) / 2 cy <- (lim$y[1] + lim$y[2]) / 2 w <- lim$x[2] - lim$x[1] h <- lim$y[2] - lim$y[1] lim <- list(x=c(cx-w, cx+w), y=c(cy-h, cy+h)) }else{ l2 <- locator(1) if(is.null(l2)) break if(sum(l$x == l2$x) || sum(l$y == l2$y)){ w <- lim$x[2] - lim$x[1] h <- lim$y[2] - lim$y[1] lim <- list(x=c(l2$x-w/2, l2$x+w/2), y=c(l2$y-h/2, l2$y+h/2)) }else lim <- list(x=sort(c(l$x, l2$x)), y=sort(c(l$y, l2$y))) } if(lim.cur < lim.depth){ lim.stack <- lim.stack[-((lim.cur+1):lim.depth),] lim.depth <- lim.cur } lim.stack <- rbind(lim.stack, c(lim$x, lim$y)) lim.depth <- lim.depth + 1 lim.cur <- lim.cur + 1 } lim }
> data col1 col2 col3 1 110 120 130 2 210 220 230 3 310 320 330 > data[, 'col1'] [1] 110 210 310 > data[, 1] [1] 110 210 310 > data[data$col1 == 110, ] col1 col2 col3 1 110 120 130
postscript("regression.ps") for(i in c("g", "a")){ for(j in 1:2){ for(k in 1:3){ data <- read.table(pipe(sprintf("head -1 regression.txt awk '/^.%s.%d%d/{print}' regression.txt | sed 's/^v/V/' | sort -r | sed 's/^V/v/' | awk '{print NR$0}'", i, j, k)), header=T) plot(data, main = sprintf(".%s.%d%d", i, j, k)) } } }
data <- read.table("file") # read table from file hist(data[[1]]) # use the first column
First, install Rdbi and RdbiPgSQL (the original Rdbi and Rdbi.PgSQL are not maintained anymore) from the shell:
R CMD INSTALL Rdbi_1.7.0.tar.gz R CMD INSTALL --configure-args='--with-pgsql-libraries=/usr/local/lib' RdbiPgSQL_1.7.1.tar.gz
Execute the following in the R command line:
library(RdbiPgSQL) conn <- dbConnect(PgSQL(), host = "myhost", dbname = "mydb") res <- dbSendQuery(conn, "select * from mytable") mydata <- dbGetResult(res)
Function | Description |
stats:shapiro.test | test for normality |
moments:agostino.test | test for skewness |
Kendall:MannKendall | non-parametric trend test for monotonic trend |