r:basics
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
r:basics [2016/09/21 08:41] – hkimscil | r:basics [2019/09/25 10:32] (current) – [Performing Vector Arithmetic] hkimscil | ||
---|---|---|---|
Line 156: | Line 156: | ||
[1] 11.49988 | [1] 11.49988 | ||
</ | </ | ||
+ | |||
+ | $$ r = \frac {\text{covariance (x, y)}} {sd(x) * sd(y)} $$ | ||
+ | |||
+ | < | ||
+ | > x <- c(0, | ||
+ | > y <- log(x+1) | ||
+ | > cor(x,y) | ||
+ | [1] 0.9068053 | ||
+ | > cov(x, | ||
+ | [1] 0.9068053 | ||
+ | > cov(x, | ||
+ | [1] 0.9068053 | ||
+ | |||
+ | </ | ||
+ | |||
< | < | ||
Line 168: | Line 183: | ||
> sd(x, na.rm=TRUE) | > sd(x, na.rm=TRUE) | ||
[1] 1.140175 | [1] 1.140175 | ||
+ | </ | ||
+ | |||
+ | <WRAP info> data | ||
+ | < | ||
+ | medium <- c(10.526448, | ||
+ | big <- c(99.83624, 100.70852, 99.73202, 98.53608, 100.74444, 98.58961, 100.46707, 99.88068, 100.46724, 100.49814) | ||
+ | |||
+ | dframe <- data.frame(small, | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > dlist <- list(small, | ||
+ | > dlist | ||
+ | [[1]] | ||
+ | [1] 0.6739635 1.5524619 0.3250562 1.2143595 1.3107692 2.1739663 | ||
+ | [7] 1.6187899 0.8872657 1.9170283 0.7767406 | ||
+ | |||
+ | [[2]] | ||
+ | [1] 10.526448 | ||
+ | | ||
+ | |||
+ | [[3]] | ||
+ | | ||
+ | [7] 100.46707 | ||
+ | > lapply (dlist, | ||
+ | [[1]] | ||
+ | [1] 1.24504 | ||
+ | |||
+ | [[2]] | ||
+ | [1] 9.560325 | ||
+ | |||
+ | [[3]] | ||
+ | [1] 99.946 | ||
+ | > sapply(dlist, | ||
+ | [1] 0.5844025 0.9920282 0.8135503 | ||
+ | |||
</ | </ | ||
Line 183: | Line 235: | ||
9 1.9170283 | 9 1.9170283 | ||
10 0.7767406 | 10 0.7767406 | ||
- | > mean(dframe) | + | > mean(dframe) |
+ | > colMeans(dframe) | ||
small medium | small medium | ||
| | ||
- | > sd(dframe) | + | > sd(dframe) # Not work. |
+ | > sd(dframe$small) | ||
+ | > sd(dframe$medium) | ||
+ | > sd(dframe$big) | ||
+ | > # OR . . . . | ||
+ | > sapply(dframe, | ||
+ | small medium | ||
+ | 0.5844025 0.9920282 0.8135503 | ||
+ | # then . . . | ||
+ | > sapply(dframe, | ||
small medium | small medium | ||
- | 0.5844025 0.9920281 0.8135498 | + | 1.245040 |
</ | </ | ||
Line 226: | Line 288: | ||
</ | </ | ||
+ | sequence ('' | ||
+ | < | ||
+ | x <- seq(-4, 4, length=10000) | ||
+ | y <- dnorm(x, mean=0, sd=1) | ||
+ | plot(x, y, type=" | ||
+ | |||
+ | </ | ||
====== Comparing Vectors ====== | ====== Comparing Vectors ====== | ||
< | < | ||
Line 240: | Line 309: | ||
> a >= pi | > a >= pi | ||
[1] FALSE | [1] FALSE | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | > a <- var(dframe) | ||
+ | > b <- cov(dframe) | ||
+ | > a == b | ||
+ | small medium | ||
+ | small | ||
+ | medium | ||
+ | big | ||
+ | > | ||
</ | </ | ||
Line 346: | Line 426: | ||
====== Performing Vector Arithmetic ====== | ====== Performing Vector Arithmetic ====== | ||
+ | see [[:social network analysis]] | ||
+ | |||
< | < | ||
> w <- c(1, | > w <- c(1, | ||
Line 374: | Line 456: | ||
[1] -1.2649111 -0.6324555 | [1] -1.2649111 -0.6324555 | ||
</ | </ | ||
+ | |||
+ | <WRAP box help>get variance of v without using var() function. | ||
+ | </ | ||
< | < | ||
Line 404: | Line 489: | ||
| <- << | | <- << | ||
| ? | Help | Recipe 1.7 | | | ? | Help | Recipe 1.7 | | ||
- | |||
< | < | ||
Line 415: | Line 499: | ||
Returns TRUE if the left operand occurs in its right operand; FALSE otherwise | Returns TRUE if the left operand occurs in its right operand; FALSE otherwise | ||
</ | </ | ||
+ | |||
+ | < | ||
+ | edge.list = matrix ( | ||
+ | c(1, | ||
+ | 2, | ||
+ | 3, | ||
+ | 4, | ||
+ | 5, | ||
+ | 6, | ||
+ | 7, | ||
+ | 8, | ||
+ | classtaken[edge.list] = 1 | ||
+ | rownames(classtaken) = c(" | ||
+ | colnames(classtaken) = c(" | ||
+ | " | ||
+ | " | ||
+ | classtaken | ||
+ | |||
+ | c = classtaken | ||
+ | tc = t(classtaken) | ||
+ | |||
+ | stu = c %*% tc | ||
+ | class = tc %*% c | ||
+ | |||
+ | stu | ||
+ | class</ | ||
+ | |||
+ | <WRAP box help> | ||
+ | fill values less than 2 with zeros in stu matrix | ||
+ | < | ||
+ | </ | ||
+ | |||
====== Defining a Function ====== | ====== Defining a Function ====== | ||
Line 442: | Line 558: | ||
+ | {{tag> statistics r "r basics" | ||
r/basics.1474416673.txt.gz · Last modified: 2016/09/21 08:41 by hkimscil