Index . 블로그창고 . Any comments?

\cat programming

Installing packages
Useful functions
Interactive zooming/panning
Accessing data in a data frame
Reading from a pipe
Drawing a histogram
Connecting to PostgreSQL
Statistical tests

http://www.r-project.org

R is an environment for data analysis.

Installing packages

install.packages("package_name")

Useful functions

FunctionReturn
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()$coeffthe coefficients of linear regression

Interactive zooming/panning

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
}

Accessing data in a data frame

> 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

Reading from a pipe

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))
		}
	}
}

Drawing a histogram

data <- read.table("file") # read table from file
hist(data[[1]])            # use the first column

Connecting to PostgreSQL

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)

Statistical tests

FunctionDescription
stats:shapiro.testtest for normality
moments:agostino.testtest for skewness
Kendall:MannKendallnon-parametric trend test for monotonic trend

All the works in this site except software and copyrighted materials by others (e.g., 문학) are licensed under a Creative Commons License. 소프트웨어 및 문학과 같이 다른 이에게 저작권이 있는 작품을 제외하고 모두 크리에이티브 커먼즈 라이센스를 따른다.
Mon Mar 25 01:47:11 2024 . XHTML . CSS (lightbox.css is not part of Uniqki. ;-) . Powered by Uniqki!
refresh . edit . loginout . index