Created on 21 June 2013
Revised on Thu Aug 15 15:34:08 2013
Study notes of Computing for Data Analysis from coursera.
x <- c(0.5, 0.6) ## numeric
x <- c(TRUE, FALSE) ## logical
x <- c(T, F) ## logical
x <- c("a", "b", "c") ## character
x <- 9:29 ## integer
x <- c(1 + (0+0i), 2 + (0+4i)) ## complex
x <- vector("numeric", length = 10) ## Using the vector() function
y <- c(1.7, "a") ## character
y <- c(TRUE, 2) ## numeric
y <- c("a", TRUE) ## character
x <- 0:6
class(x)
## [1] "integer"
as.numeric(x)
## [1] 0 1 2 3 4 5 6
as.logical(x)
## [1] FALSE TRUE TRUE TRUE TRUE TRUE TRUE
as.character(x)
## [1] "0" "1" "2" "3" "4" "5" "6"
as.complex(x)
## [1] 0+0i 1+0i 2+0i 3+0i 4+0i 5+0i 6+0i
x <- c("a", "b", "c")
as.numeric(x) ## Nonsensical coercion results in NAs
## Warning: 强制改变过程中产生了NA
## [1] NA NA NA
as.logical(x) ## Nonsensical coercion results in NAs
## [1] NA NA NA
m <- matrix(nrow = 2, ncol = 3)
m
## [,1] [,2] [,3]
## [1,] NA NA NA
## [2,] NA NA NA
dim(m)
## [1] 2 3
attributes(m)
## $dim
## [1] 2 3
m <- matrix(1:6, nrow = 2, ncol = 3)
m
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
m <- 1:10
m
## [1] 1 2 3 4 5 6 7 8 9 10
dim(m) <- c(2, 5)
m
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10
x <- 1:3
y <- 10:12
cbind(x, y) ## column-binding
## x y
## [1,] 1 10
## [2,] 2 11
## [3,] 3 12
rbind(x, y) ## row-binding
## [,1] [,2] [,3]
## x 1 2 3
## y 10 11 12
x <- list(1, "a", TRUE, 1 + (0+4i))
x
## [[1]]
## [1] 1
##
## [[2]]
## [1] "a"
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] 1+4i
x <- factor(c("yes", "yes", "no", "yes", "no"))
x
## [1] yes yes no yes no
## Levels: no yes
table(x)
## x
## no yes
## 2 3
unclass(x)
## [1] 2 2 1 2 1
## attr(,"levels")
## [1] "no" "yes"
x <- factor(c("yes", "yes", "no", "yes", "no"), levels = c("yes", "no"))
x ## The order of the levels can be set using the levels argument
## [1] yes yes no yes no
## Levels: yes no
x <- c(1, 2, NA, 10, 3)
is.na(x)
## [1] FALSE FALSE TRUE FALSE FALSE
is.nan(x)
## [1] FALSE FALSE FALSE FALSE FALSE
x <- c(1, 2, NaN, NA, 4)
is.na(x)
## [1] FALSE FALSE TRUE TRUE FALSE
is.nan(x)
## [1] FALSE FALSE TRUE FALSE FALSE
x <- data.frame(foo = 1:4, bar = c(T, T, F, F))
x
## foo bar
## 1 1 TRUE
## 2 2 TRUE
## 3 3 FALSE
## 4 4 FALSE
nrow(x)
## [1] 4
ncol(x)
## [1] 2
x <- 1:3
names(x)
## NULL
names(x) <- c("foo", "bar", "norf")
x
## foo bar norf
## 1 2 3
names(x)
## [1] "foo" "bar" "norf"
x <- list(a = 1, b = 2, c = 3) ## Lists can also have names
x
## $a
## [1] 1
##
## $b
## [1] 2
##
## $c
## [1] 3
m <- matrix(1:4, nrow = 2, ncol = 2)
dimnames(m) <- list(c("a", "b"), c("c", "d")) ## matrix can also have names
x <- c("a", "b", "c", "c", "d", "a")
x[1]
## [1] "a"
x[2]
## [1] "b"
x[1:4]
## [1] "a" "b" "c" "c"
x[x > "a"]
## [1] "b" "c" "c" "d"
u <- x > "a"
u
## [1] FALSE TRUE TRUE TRUE TRUE FALSE
x[u]
## [1] "b" "c" "c" "d"
x <- matrix(1:6, 2, 3)
x ## Matrices can be subsetted in the usual way with (i , j) type indices
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
x[1, 2] ## row 1, col 2, returned as a vector of length 1
## [1] 3
x[1, 2, drop = FALSE] ## row 1, col 2, returned as a 1 ?? 1 matrix
## [,1]
## [1,] 3
x[2, 1] ## row 2, col 1
## [1] 2
x[1, ] ## Indices can also be missing. row 1
## [1] 1 3 5
x[, 2] ## Indices can also be missing. col 2
## [1] 3 4
x[, 2, drop = FALSE]
## [,1]
## [1,] 3
## [2,] 4
x <- list(foo = 1:4, bar = 0.6, baz = "hello")
x[1]
## $foo
## [1] 1 2 3 4
x[[1]]
## [1] 1 2 3 4
x$bar
## [1] 0.6
x[["bar"]]
## [1] 0.6
x["bar"]
## $bar
## [1] 0.6
x[c(1, 3)]
## $foo
## [1] 1 2 3 4
##
## $baz
## [1] "hello"
name <- "foo"
x[[name]] ## computed index for ??foo??
## [1] 1 2 3 4
x$name ## element ??name?? doesn??t exist!
## NULL
x$foo ## element ??foo?? does exist
## [1] 1 2 3 4
x <- list(a = list(10, 12, 14), b = c(3.14, 2.81))
x[[c(1, 3)]]
## [1] 14
x[[1]][[3]]
## [1] 14
x[[c(2, 1)]]
## [1] 3.14
x <- list(aardvark = 1:5)
x$a ## Partial matching of names is allowed with [[ and $.
## [1] 1 2 3 4 5
x[["a"]]
## NULL
x[["a", exact = FALSE]] ## Partial matching of names is allowed with [[ and $.
## [1] 1 2 3 4 5
x <- c(1, 2, NA, 4, NA, 5)
bad <- is.na(x)
x[!bad]
## [1] 1 2 4 5
x <- c(1, 2, NA, 4, NA, 5)
y <- c("a", "b", NA, "d", NA, "f")
good <- complete.cases(x, y)
good
## [1] TRUE TRUE FALSE TRUE FALSE TRUE
x[good]
## [1] 1 2 4 5
y[good]
## [1] "a" "b" "d" "f"
airquality[1:6, ]
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
good <- complete.cases(airquality)
airquality[good, ][1:6, ]
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 7 23 299 8.6 65 5 7
## 8 19 99 13.8 59 5 8
x <- matrix(1:4, 2, 2)
y <- matrix(rep(10, 4), 2, 2)
x * y ## element-wise multiplication
## [,1] [,2]
## [1,] 10 30
## [2,] 20 40
x/y
## [,1] [,2]
## [1,] 0.1 0.3
## [2,] 0.2 0.4
x %*% y ## true matrix multiplication
## [,1] [,2]
## [1,] 40 40
## [2,] 60 60
data <- read.table("foo.txt")
con <- gzfile("words.gz")
x <- readLines(con, 10) ## read lines of a text file and store them in a character vector.
con <- url("http://www.jhsph.edu", "r")
x <- readLines(con)
x = 5
if (x > 3) {
y <- 10
} else {
y <- 0
}
y
## [1] 10
Of course, the else clause is not necessary.
for (i in 1:10) {
print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
x <- c("a", "b", "c", "d")
for (i in 1:4) {
print(x[i])
}
## [1] "a"
## [1] "b"
## [1] "c"
## [1] "d"
for (i in seq_along(x)) {
print(x[i])
}
## [1] "a"
## [1] "b"
## [1] "c"
## [1] "d"
for (letter in x) {
print(letter)
}
## [1] "a"
## [1] "b"
## [1] "c"
## [1] "d"
for (i in 1:4) print(x[i])
## [1] "a"
## [1] "b"
## [1] "c"
## [1] "d"
x <- matrix(1:6, 2, 3)
for (i in seq_len(nrow(x))) {
for (j in seq_len(ncol(x))) {
print(x[i, j])
}
}
## [1] 1
## [1] 3
## [1] 5
## [1] 2
## [1] 4
## [1] 6
count <- 0
while (count < 10) {
print(count)
count <- count + 1
}
## [1] 0
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
z <- 5
while (z >= 3 && z <= 10) {
print(z)
coin <- rbinom(1, 1, 0.5)
if (coin == 1) {
## random walk
z <- z + 1
} else {
z <- z - 1
}
}
## [1] 5
## [1] 4
## [1] 3
## [1] 4
## [1] 3
next is used to skip an iteration of a loop return signals that a function should exit and return a given val
for (i in 1:5) {
if (i <= 3) {
next
}
print(i)
}
## [1] 4
## [1] 5
mydata <- rnorm(100)
sd(mydata)
## [1] 0.8927
sd(x = mydata)
## [1] 0.8927
sd(x = mydata, na.rm = FALSE)
## [1] 0.8927
sd(na.rm = FALSE, x = mydata)
## [1] 0.8927
sd(na.rm = FALSE, mydata)
## [1] 0.8927
f <- function(a, b) {
print(a)
print(b)
}
f(45) ## Arguments to functions are evaluated lazily, so they are evaluated only as needed.
## [1] 45
## Error: 缺少参数"b",也没有缺省值
make.power <- function(n) {
pow <- function(x) {
x^n
}
pow
}
cube <- make.power(3)
square <- make.power(2)
cube(3)
## [1] 27
square(3)
## [1] 9
y <- 10
f <- function(x) {
y <- 2
y^2 + g(x)
}
g <- function(x) {
x * y
}
f(3)
## [1] 34
lapply: Loop over a list and evaluate a function on each element sapply: Same as lapply but try to simplify the result apply: Apply a function over the margins of an array tapply: Apply a function over subsets of a vector mapply: Multivariate version of lapply
lapply takes three arguments: a list X, a function (or the name of a function) FUN, and other arguments via its … argument. If X is not a list, it will be coerced to a list using as.list.
x <- list(a = 1:5, b = rnorm(10))
lapply(x, mean) ## lapply always returns a list, regardless of the class of the input.
## $a
## [1] 3
##
## $b
## [1] -0.4471
x <- list(a = 1:4, b = rnorm(10), c = rnorm(20, 1), d = rnorm(100, 5))
lapply(x, mean)
## $a
## [1] 2.5
##
## $b
## [1] -0.397
##
## $c
## [1] 1.385
##
## $d
## [1] 5.033
x <- 1:4
lapply(x, runif)
## [[1]]
## [1] 0.2566
##
## [[2]]
## [1] 0.06697 0.24641
##
## [[3]]
## [1] 0.3426 0.5550 0.5669
##
## [[4]]
## [1] 0.1136 0.2403 0.4380 0.2106
x <- 1:4
lapply(x, runif, min = 0, max = 10)
## [[1]]
## [1] 3.193
##
## [[2]]
## [1] 4.086 5.373
##
## [[3]]
## [1] 2.242 4.797 8.987
##
## [[4]]
## [1] 5.876 9.611 3.617 6.057
x <- list(a = matrix(1:4, 2, 2), b = matrix(1:6, 3, 2))
lapply(x, function(elt) elt[, 1])
## $a
## [1] 1 2
##
## $b
## [1] 1 2 3
sapply will try to simplify the result of lapply if possible.
If the result is a list where every element is length 1, then a vector is returned
If the result is a list where every element is a vector of the same length (> 1), a matrix is returned.
If it can??t figure things out, a list is returned
x <- list(a = 1:4, b = rnorm(10), c = rnorm(20, 1), d = rnorm(100, 5))
lapply(x, mean)
## $a
## [1] 2.5
##
## $b
## [1] -0.1568
##
## $c
## [1] 1.137
##
## $d
## [1] 4.964
sapply(x, mean)
## a b c d
## 2.5000 -0.1568 1.1368 4.9642
mean(x)
## Warning: argument is not numeric or logical: returning NA
## [1] NA
apply is used to a evaluate a function (often an anonymous one) over the margins of
an array.
It is most often used to apply a function to the rows or columns of a matrix
It can be used with general arrays, e.g. taking the average of an array of matrices
It is not really faster than writing a loop, but it works in one line!
function (X, MARGIN, FUN, …)
X is an array
MARGIN is an integer vector indicating which margins should be ??retained??.
FUN is a function to be applied
… is for other arguments to be passed to FUN
x <- matrix(rnorm(200), 20, 10)
dim(x)
## [1] 20 10
apply(x, 2, mean) ## col
## [1] 0.1593047 0.1364883 -0.0006164 0.0630158 0.2437599 -0.0269508
## [7] 0.1934873 -0.0458485 -0.2129331 0.2378690
apply(x, 1, sum) ## row
## [1] 2.76976 1.00911 3.22360 3.45751 0.07282 0.03905 2.87195
## [8] -3.16416 -4.77845 0.26958 3.65916 1.10861 3.01184 0.22964
## [15] 1.78231 1.11114 -0.54030 -0.93820 1.86032 -2.10374
For sums and means of matrix dimensions, we have some shortcuts.
rowSums = apply(x, 1, sum)
rowMeans = apply(x, 1, mean)
colSums = apply(x, 2, sum)
colMeans = apply(x, 2, mean)
rowSums(x)
## [1] 2.76976 1.00911 3.22360 3.45751 0.07282 0.03905 2.87195
## [8] -3.16416 -4.77845 0.26958 3.65916 1.10861 3.01184 0.22964
## [15] 1.78231 1.11114 -0.54030 -0.93820 1.86032 -2.10374
rowMeans(x)
## [1] 0.276976 0.100911 0.322360 0.345751 0.007282 0.003905 0.287195
## [8] -0.316416 -0.477845 0.026958 0.365916 0.110861 0.301184 0.022964
## [15] 0.178231 0.111114 -0.054030 -0.093820 0.186032 -0.210374
colSums(x)
## [1] 3.18609 2.72977 -0.01233 1.26032 4.87520 -0.53902 3.86975
## [8] -0.91697 -4.25866 4.75738
colMeans(x)
## [1] 0.1593047 0.1364883 -0.0006164 0.0630158 0.2437599 -0.0269508
## [7] 0.1934873 -0.0458485 -0.2129331 0.2378690
x <- matrix(rnorm(200), 20, 10)
apply(x, 1, quantile, probs = c(0.25, 0.75))
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## 25% -0.4864 -0.5553 -1.0450 -0.3781 -0.887 -0.3922 -1.08306 -0.4040
## 75% 0.8825 0.3214 0.1539 0.5993 1.222 0.4654 -0.07759 0.2677
## [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
## 25% -0.3508 -0.2736 -0.5975 -0.5708 -0.569 -0.3983 -0.6999 -0.52519
## 75% 0.7366 0.8194 0.7215 1.0494 0.370 0.9677 0.2261 -0.01286
## [,17] [,18] [,19] [,20]
## 25% -0.06186 -0.7434 -0.9679 -0.6756
## 75% 0.88176 0.5834 0.7979 0.1181
tapply is used to apply a function over subsets of a vector
function (X, INDEX, FUN = NULL, …, simplify = TRUE)
X is a vector
INDEX is a factor or a list of factors (or else they are coerced to factors)
FUN is a function to be applied
… contains other arguments to be passed FUN
simplify, should we simplify the result?
x <- c(rnorm(10), runif(10), rnorm(10, 1))
f <- gl(3, 10)
f
## [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3
## Levels: 1 2 3
tapply(x, f, mean)
## 1 2 3
## 0.2363 0.3670 0.9093
tapply(x, f, mean, simplify = FALSE)
## $`1`
## [1] 0.2363
##
## $`2`
## [1] 0.367
##
## $`3`
## [1] 0.9093
tapply(x, f, range)
## $`1`
## [1] -1.303 1.443
##
## $`2`
## [1] 0.002004 0.994638
##
## $`3`
## [1] -0.6496 2.0582
split takes a vector or other objects and splits it into groups determined by a factor or list of factors.
str(split)
function (x, f, drop = FALSE, …)
x is a vector (or list) or data frame
f is a factor (or coerced to one) or a list of factors
drop indicates whether empty factors levels should be dropped
x <- c(rnorm(10), runif(10), rnorm(10, 1))
f <- gl(3, 10)
split(x, f)
## $`1`
## [1] 0.9276 1.4945 -1.1914 0.8772 0.4115 -1.4497 -1.4979 1.0045
## [9] 0.7356 0.6245
##
## $`2`
## [1] 0.14352 0.69423 0.71899 0.70204 0.07574 0.97746 0.20206 0.76935
## [9] 0.10761 0.51644
##
## $`3`
## [1] -1.0172 -0.2693 1.8225 1.2192 0.6786 -0.7900 1.4902 1.6970
## [9] 1.8241 2.4860
lapply(split(x, f), mean)
## $`1`
## [1] 0.1936
##
## $`2`
## [1] 0.4907
##
## $`3`
## [1] 0.9141
mapply is a multivariate apply of sorts which applies a function in parallel over a set of
arguments.
str(mapply)
function (FUN, …, MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE)
FUN is a function to apply
… contains arguments to apply over
MoreArgs is a list of other arguments to FUN.
SIMPLIFY indicates whether the result should be simplified
mapply(rep, 1:4, 4:1)
## [[1]]
## [1] 1 1 1 1
##
## [[2]]
## [1] 2 2 2
##
## [[3]]
## [1] 3 3
##
## [[4]]
## [1] 4
Functions for probability distributions in R:
rnorm: generate random Normal variates with a given mean and standard deviation
dnorm: evaluate the Normal probability density (with a given mean/SD) at a point (or vector of points)
pnorm: evaluate the cumulative distribution function for a Normal distribution
rpois: generate random Poisson variates with a given rate
x <- rnorm(10)
summary(x)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -2.150 -1.350 -0.761 -0.245 0.978 2.590
x <- rnorm(10, 20, 2)
summary(x)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 19.2 19.5 20.1 20.8 21.0 26.2
set.seed(1) ## Setting the random number seed with set.seed ensures reproducibility
rnorm(5) ## Always set the random number seed when conducting a simulation!
## [1] -0.6265 0.1836 -0.8356 1.5953 0.3295
rpois(10, 1)
## [1] 0 0 1 1 2 1 1 4 1 2
rpois(10, 2)
## [1] 4 1 2 0 1 1 0 1 4 1
rpois(10, 20)
## [1] 19 19 24 23 22 24 23 20 11 22
ppois(2, 2) ## Cumulative distribution
## [1] 0.6767
ppois(4, 2)
## [1] 0.9473
ppois(6, 2)
## [1] 0.9955
set.seed(20)
x <- rnorm(100)
e <- rnorm(100, 0, 2)
y <- 0.5 + 2 * x + e
summary(y)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -6.410 -1.540 0.679 0.689 2.930 6.510
plot(x, y)
What if x is binary?
set.seed(10)
x <- rbinom(100, 1, 0.5)
e <- rnorm(100, 0, 2)
y <- 0.5 + 2 * x + e
summary(y)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -3.490 -0.141 1.580 1.430 2.840 6.940
plot(x, y)
set.seed(1)
x <- rnorm(100)
log.mu <- 0.5 + 0.3 * x
y <- rpois(100, exp(log.mu))
summary(y)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.00 1.00 1.00 1.55 2.00 6.00
plot(x, y)
set.seed(1)
sample(1:10, 4)
## [1] 3 4 5 7
sample(1:10, 4)
## [1] 3 9 8 5
sample(letters, 5)
## [1] "q" "b" "e" "x" "p"
sample(1:10) ## permutation
## [1] 4 7 10 6 9 2 8 3 1 5
sample(1:10)
## [1] 2 3 4 1 9 5 10 8 6 7
sample(1:10, replace = TRUE) ## Sample w/replacement
## [1] 2 9 7 8 2 8 5 9 7 8
plot: make a scatterplot, or other type of plot depending on the class of the object being plotted
lines: add lines to a plot, given a vector x values and a corresponding vector of y values (or a 2-column matrix); this function just connects the dots
points: add points to a plot
text: add text labels to a plot using specified x, y coordinates
title: add annotations to x, y axis labels, title, subtitle, outer margin
mtext: add arbitrary text to the margins (inner or outer) of the plot
axis: adding axis ticks/labels
xyplot: this is the main function for creating scatterplots
bwplot: box-and-whiskers plots (??boxplots??)
histogram: histograms
stripplot: like a boxplot but with actual points
dotplot: plot dots on ??violin strings??
splom: scatterplot matrix; like pairs in base graphics system
levelplot, contourplot: for plotting ??image?? data
library(lattice)
plots y vs. x conditioned on f.
x <- rnorm(100)
y <- x + rnorm(100, sd = 0.5)
f <- gl(2, 50, labels = c("Group 1", "Group 2"))
xyplot(y ~ x | f)
plots y vs. x conditioned on f with horizontal (dashed) line drawn at the median of y for each panel.
xyplot(y ~ x | f, panel = function(x, y, ...) {
panel.xyplot(x, y, ...)
panel.abline(h = median(y), lty = 2)
})
Adding a regression line
xyplot(y ~ x | f, panel = function(x, y, ...) {
panel.xyplot(x, y, ...)
panel.lmline(x, y, col = 2)
})
x <- rnorm(10000)
y <- rnorm(10000)
smoothScatter(x, y)
## KernSmooth 2.23 loaded Copyright M. P. Wand 1997-2009
plot(x, y, pch = 19) ## ScaNerplot with no transparency
plot(x, y, col = rgb(0, 0, 0, 0.2), pch = 19) ## ScaNerplot with transparency
Some metacharacters represent the start of a line: ^
Some metacharacters represent the end of a line: $
We can list a set of characters we will accept at a given point in the match: []
any character: .
or: |
and: Subexpressions are often contained in parentheses to constrain the alternatives
The question mark indicates that the indicated expression is optional: ?
any number, including none of the item: *
at least one of the item: +
specify the minimum and maximum number of matches of an expression: {min,max}
specify a range of letters [a-z] or [a-zA-Z0-9], notice that the order doesn't matter
When used at the beginning of a character class, the "^" is also a metacharacter and indicates matching characters NOT in the indicated class
Search for matches of a regular expression/pattern in a character vector; either return the indices into the character vector that match, the strings that happen to match, or a TRUE/FALSE vector indicating which elements match
Search a character vector for regular expression matches and return the indices of the string where the match begins and the length of the match
Search a character vector for regular expression matches and replace that match with another string
Gives you indices of parethensized sub-expressions.
x <- as.Date("1970-01-01")
x
## [1] "1970-01-01"
weekdays: give the day of the week
months: give the month name
quarters: give the quarter number (“Q1”, “Q2”, “Q3”, or “Q4”)
x <- Sys.time()
x
## [1] "2013-08-15 15:34:12 CST"
p <- as.POSIXlt(x)
names(unclass(p))
## [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst"
p$sec
## [1] 12.67
x <- as.Date("2012-01-01")
x <- as.POSIXlt(x)
y <- strptime("9 Jan 2011 11:34:21", "%d %b %Y %H:%M:%S")
x - y
## Time difference of NA secs
x <- as.Date("2012-03-01")
y <- as.Date("2012-02-28")
x - y
## Time difference of 2 days
x <- as.POSIXct("2012-10-25 01:00:00")
y <- as.POSIXct("2012-10-25 06:00:00", tz = "GMT")
y - x
## Time difference of 13 hours
S3 classes/methods
S4 classes/methods
set.seed(10)
x <- rnorm(100)
x <- as.ts(x) ## Convert to a time series object
plot(x)
setClass("polygon", representation(x = "numeric", y = "numeric"))
setMethod("plot", "polygon", function(x, y, ...) {
plot(x@x, x@y, type = "n", ...)
xp <- c(x@x, x@x[1])
yp <- c(x@y, x@y[1])
lines(xp, yp)
})
## Creating a generic function for 'plot' from package 'graphics' in the
## global environment
## [1] "plot"
p <- new("polygon", x = c(1, 2, 3, 4), y = c(1, 2, 3, 1))
plot(p)