Computer Scientist, Graduate Student, and Geek

Tag: R

Replacing Values in a Vector with NA in R

October 01, 2012

# Load data files
dataA <- read.table("dataA.csv", header=TRUE, sep=",")

# Replace zeros in column ColA with NA
dataA$ColA <- replace(dataA$ColA, data$ColA == 0, NA)

Categories: Uncategorized

Tags: R


Simple CDF Graph in R

August 10, 2012

Below is a sample script for R for creating a cumulative distribution frequency (CDF) graph with one or more curves.

# Load data files
dataA <- read.table("dataA.csv", header=TRUE, sep=",")

# Create CDF points
count <- length(dataA$ColA)-1
cdf.dataA <- data.frame(x=sort(dataA$ColA), y=c(0:count)/count)

# Setup graph parameters
xrange <- c(0, 10)
yrange <- c(0,1)
colors <- c('black') # 'gray39', 'gray69'
linelabels <- c('DataA')
linetype <-c (1)

# Setup output
postscript("cdf.ps", width=5, height=4)
#png("cdf.png", width=400, height=300)

# Setup basic plot
plot(xrange, yrange, type='n', main='', xlab='ColA', ylab='CDF')

# Add lines to plot
lines(cdf.dataA$x, cdf.dataA$y, type='l', col=colors[1], lwd=2,
    lty=linetype[1])

# Add legend
legend(xrange[1], yrange[2], col=colors, lwd=2, lty=linetype,
    legend=linelabels)

# End output
dev.off()

Categories: Uncategorized

Tags: R


Installing Packages in R

March 07, 2012

You can install packages in R by running the following command within R:

install.packages(c("pkg1", "pkg2"))

(See http://cran.r-project.org/doc/manuals/R-admin.html#Installing-packages for more information.)

Categories: Uncategorized

Tags: R