lines <- readLines( )
lines <- readLines( )terminate the input with Control-Z for Windows; Control-D for Mac and Unix. For example:
> lines < readLines( ) This is a test. Control-Z > lines [1] "This" "is" "a" "test."Terminating the keyboard input with Control-Z does not work in RStudio, but you can specify the number of lines to read like this:
> lines <- readLines(n=2) This is >Ther readLines function also reads from a file:
lines <- readLines("kids.txt")
kids <- read.table("kids.txt", header=F)Ans: The header is treated as data and the default variables V1, V2, V3, etc. are used for the columns. You can add column names like this:
kids <- read.table("kids.txt", header=F, col.names=c("Age", "Gender", "Age"))
> v <- c(2, 4, 3, 4, 2, 3, 2, 3) > table(v) # Show frequency of outcomes in v. v 2 3 4 3 3 2 > w <- c(1, 3, 1, 1, 3, 3, 1, 1) > # Show 3x2 contingency table of > table(v, w) # outcomes of v and w. w v 1 3 2 2 1 3 2 1 4 1 1 > # Higher dimensional contingency tables are possible.
values <- factor(c(7, 3, 9, 2)) df <- data.frame(n=1:4, a=values) plot(df)What is wrong?
plot(df[ ,1], as.character(df[ ,2]))Of course if the values 7, 3, 9, 2 were entered in column 2 of the data frame instead, these values would not be stored as factors, so the dataframe could be plotted as
plot(df[ ,1], df[ ,2])or, even easier,
plot(df)
kid <- function(theName, theGender, theAge) { theObject <- list(name=theName, gender=theGender, age=theAge) class(theObject) = "Kid" return(theObject) }
haveBirthday <- function(theObject) { UseMethod("haveBirthday", theObject) }This generic function determines the class of the object that is calling it and then calls the specific haveBirthday method for that class, in this case haveBirthday.Kid.
v <- c(45, 31, 19, 76, 111)using methods 1 and 4. Verify your results by hand.
19 21 45 76 111Compute the 0.67 quartile = Q(0.67) using Method 1:
Index: | 1 | 2 | 3 | 4 | 5 |
Quantile: | 0.2 | 0.4 | 0.6 | 0.8 | 1.0 |
Value: | x1=19 | x2=21 | x3=45 | x4=76 | x5=111 |
> quantile(v, 0.67, type=1) 67% 76 > quantile(v, 0.67, type=4) 67% 55.85 >Here is the R script that draws the interpolation diagram.
# Compute quantile using R type 4 calculation. myQuantileType4(data, p) { data <- sort(data) n <- length(data) j <- floor(p * n) pdiff_big = 1 / n xdiff_big = data[j + 1] - data[j] pdiff_small = p - j / n; xdiff_small = xdiff_big * pdiff_small / pdiff_big; return(data[j] + xdiff_small) } x <- c(19, 21, 45, 76, 111) myQuantileType4(x, 0.67) quantile(x, 0.67, type=4)
R | SAS | |||
---|---|---|---|---|
Normal | Uniform | Normal | Uniform | |
Density (PDF) | dnorm | dunif | pdf("normal", ...) | pdf("uniform", ...) |
Cumulative (CDF) | pnorm | punif | cdf("normal", ...) | cdf("uniform", ...) |
Quantile (IDF) | qnorm | qunif | quantile("normal", ...) | quantile("uniform", ...) |
Random Variable (RV) | rnorm | runif | rand("normal", ...) | rand("uniform", ...) |
> print(pnorm(1:3, mean=0, sd=1) - pnorm(-(1:3), mean=0, sd=1)) [1] 0.6826895 0.9544997 0.9973002
> p = c(0.95, 0.99, 0.999) > print(qnorm(1 - (1 - p) / 2, mean=0, sd=1)) [1] 1.959964 2.575829 3.290527;
> x <- rnorm(100000, mean=100, sd=15) > mean(x) [1] 0.003009677 > sd(x) [1] 1.003269Note: modern normal random number generators are based on a good uniform random number generator. Most popular languages, including R and SAS use the Mersenne-Twister random number generator, invented by Matsumoto and Nishimura in 1998.
install.packages("haven")
library(haven) kids <- read_sas("kids.sas7bdat")
DBF Epi Info Minitab Octave SPSS Stata Systat
library(rjson) kids <- fromJSON("kids.json")
# JSON Example # Read a JSON file and convert it to a list. library(rjson) k <- fromJSON(file="kids.json", method = "C", unexpected.escape = "skip") # Convert list to dataframe. kidsdf <- as.data.frame(k[[1]]) len <- length(k) for(i in 2:len) { kidsdf <- rbind(kidsdf, as.data.frame(k[[i]])) } # Print resulting dataframe print(kidsdf)
aprof batch BatchExperiments BatchJobs bayesm bcp biglars biglm bigmemory bigrf biopara bnlearn caret cudaBayesreg data.table dclone doMC doMPI doRedis doRNG doSNOW ff ffbase foreach GAMBoost gcbd Geneland gmatrix gputools GUIProfiler HadoopStreaming harvestr HiPLARM HistogramTools inline latentnet lga magma Matching MonetDB.R nws OpenCL orloca pbdBASE pbdDEMO pbdDMAT pbdMPI pbdNCDF4 pbdPROF pbdSLAP peperr permGPU PGICA pls pmclust profr proftools pvclust Rborist Rcpp Rdsm rgenoud Rhpc RhpcBLASctl RInside rJava rlecuyer Rmpi (core) RProtoBuf rpud rredis snow (core) snowfall snowFT speedglm sqldf STAR tm varSelRF WideLM
# Load parallel library. library(parallel) # Make a cluster consisting of the eight CPUs # on the machine. clust <- makeCluster(getOption("cl.cores", 8)) # Create input matrix. M <- matrix(1:64, 8, 8, byrow=T) # Sum rows of matrix using parRapply: sums <- parRapply(clust, M, sum) print(sums) print(sum(sums))
install.packages("ggplot2")
simple_dat <- data.frame( Aval = c("A1", "A1", "A2", "A2", "A3", "A3"), Bval = c("B1", "B2", "B1", "B2", "B1", "B2"), value = c(10, 9, 7, 11, 12, 6) ) library(ggplot2) # Create bar graph using ggplot and geom_bar. ggplot(simple_dat, aes(x=Aval, y=value, fill=Bval)) + geom_bar(stat="identity", position="dodge") # Create bar graph from same data, but with x and fill # mappings switched. ggplot(simple_dat, aes(x=Bval, y=value, fill=Aval)) + geom_bar(stat="identity", position="dodge") # Create line graph made with ggplot and geom_line. ggplot(simple_dat, aes(x=Aval, y=value, colour=Bval, group=Bval)) + geom_line( )
proc sgplot data = maps.namerica
noautolegend;
scatter x = x y = y /
group = id markerattrs=(size=1);
xaxis grid label = ' ';
yaxis grid label = ' ';
myConcat <- function(u, v) { lenu <- length(u) lenv <- length(v) w <- rep(0, lenu + lenv) for(i in 1:lenu) { w[i] <- u[i] } for(i in 1:lenv) { w[i + lenu] <- v[i] } return(w) } a <- c(1, 3, 5, 7, 9) b <- c(8, 6, 4, 2) myConcat(a, b)
Name | IDE Button | Function Key | Description |
---|---|---|---|
Next | F10 | Execute the next line of code. | |
Step Into | Shift-F4 | Step into the current function call. | |
Execute Remainder |
Shift-F6 | Execute remainder of current function call or loop. | |
Continue | Shift-F5 | Continue execution until the next breakpoint is encountered. | |
Stop | Shift-F8 | Exit debug mode. |