Last active
August 29, 2015 14:16
-
-
Save anantjain-xyz/82b5bc7b58bc0aa0fe4e to your computer and use it in GitHub Desktop.
Small snippets of R code written while learning R.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> data.frame(a=1:5, b=6:10) | |
a b | |
1 1 6 | |
2 2 7 | |
3 3 8 | |
4 4 9 | |
5 5 10 | |
> data.frame(a=1:5, b=6:9)[["a"]] | |
Error in data.frame(a = 1:5, b = 6:9) : | |
arguments imply differing number of rows: 5, 4 | |
> list(a=1, b=2)[["a"]] | |
[1] 1 | |
> list(a=1, b=2)[[1]] | |
[1] 1 | |
> list(a=1, b=2)[1] | |
$a | |
[1] 1 | |
> matrix(0, nrow=2, ncol=3) | |
[,1] [,2] [,3] | |
[1,] 0 0 0 | |
[2,] 0 0 0 | |
> matrix(c(0, 1), nrow=2, ncol=3) | |
[,1] [,2] [,3] | |
[1,] 0 0 0 | |
[2,] 1 1 1 | |
> matrix(c(0, 1), nrow=2, ncol=3, byrow=TRUE) | |
[,1] [,2] [,3] | |
[1,] 0 1 0 | |
[2,] 1 0 1 | |
> m <- matrix(c(0, 1), nrow=2, ncol=3) | |
> m * m | |
[,1] [,2] [,3] | |
[1,] 0 0 0 | |
[2,] 1 1 1 | |
> m %*% t(m) | |
[,1] [,2] | |
[1,] 0 0 | |
[2,] 0 3 | |
> a <- if (TRUE) 1 else 2 | |
> a | |
[1] 1 | |
> a <- if (FALSE) 1 else 2 | |
> a | |
[1] 2 | |
> f <- function() { | |
+ 1 | |
+ } | |
> f() | |
[1] 1 | |
> lapply | |
function (X, FUN, ...) | |
{ | |
FUN <- match.fun(FUN) | |
if (!is.vector(X) || is.object(X)) | |
X <- as.list(X) | |
.Internal(lapply(X, FUN)) | |
} | |
<bytecode: 0x101a432a8> | |
<environment: namespace:base> | |
> ?lapply | |
starting httpd help server ... done | |
> lapply(1:5, function(x) x*x) | |
[[1]] | |
[1] 1 | |
[[2]] | |
[1] 4 | |
[[3]] | |
[1] 9 | |
[[4]] | |
[1] 16 | |
[[5]] | |
[1] 25 | |
> sapply(1:5, function(x) x*x) | |
[1] 1 4 9 16 25 | |
> sapply(1:5, function(x) c(x*x, x)) | |
[,1] [,2] [,3] [,4] [,5] | |
[1,] 1 4 9 16 25 | |
[2,] 1 2 3 4 5 | |
> apply(m, 1, sum) | |
[1] 0 3 | |
> m | |
[,1] [,2] [,3] | |
[1,] 0 0 0 | |
[2,] 1 1 1 | |
> apply(m, 2, sum) | |
[1] 1 1 1 | |
> df <- data.frame(a=1:5, b=6:10) | |
> df | |
a b | |
1 1 6 | |
2 2 7 | |
3 3 8 | |
4 4 9 | |
5 5 10 | |
> apply(df, 1, sum) | |
[1] 7 9 11 13 15 | |
> df <- data.frame(a=paste0(as.character(1:5), "str"), b=6:10) | |
> df | |
a b | |
1 1str 6 | |
2 2str 7 | |
3 3str 8 | |
4 4str 9 | |
5 5str 10 | |
> head(df) | |
a b | |
1 1str 6 | |
2 2str 7 | |
3 3str 8 | |
4 4str 9 | |
5 5str 10 | |
> tail(df) | |
a b | |
1 1str 6 | |
2 2str 7 | |
3 3str 8 | |
4 4str 9 | |
5 5str 10 | |
> unlist(lapply(1:5, function(x) x*x)) | |
[1] 1 4 9 16 25 | |
> length(1:5) | |
[1] 5 | |
> length(m) | |
[1] 6 | |
> dim(m) | |
[1] 2 3 | |
> dim(df) | |
[1] 5 2 | |
> length(df) | |
[1] 2 | |
> mode(df) | |
[1] "list" | |
> df$a | |
[1] 1str 2str 3str 4str 5str | |
Levels: 1str 2str 3str 4str 5str | |
> df[["a"]] | |
[1] 1str 2str 3str 4str 5str | |
Levels: 1str 2str 3str 4str 5str | |
> as.numeric(df$a) | |
[1] 1 2 3 4 5 | |
#The above as.numeric shows how R stores those strings as ints. It's like ENUM of C - it's on by default - so you can switch it off. | |
> names(df) | |
[1] "a" "b" | |
> table(c(1, 2, 3, 1, 2)) | |
1 2 3 | |
2 2 1 | |
# This is like a histogram | |
> t <- table(c(1, 2, 3, 1, 2)) | |
> t["1"] | |
1 | |
2 | |
> t["2"] | |
2 | |
2 | |
#So here, R is storing each of those numbers as a string ENUM, and counting it. | |
> names(t) | |
[1] "1" "2" "3" | |
> as.vector(t) | |
[1] 2 2 1 | |
> f <- function() { b <- 5 } | |
> b <- 2 | |
> b | |
[1] 2 | |
> f() | |
> b | |
[1] 2 | |
> f<- function() { b <<- 5 } | |
> f() | |
> b | |
[1] 5 | |
ls() | |
#prints all the vars in the working memory | |
> match(1:5, c(1, 0, 2, 0, 3, 0, 4)) | |
[1] 1 3 5 7 NA | |
> unique(c(1, 2, 1)) | |
[1] 1 2 | |
> mode(data[[1]]) | |
[1] "list" | |
> length(data[[1]]) | |
[1] 990 | |
> sort(c(2, 1, 3, 5, 4)) | |
[1] 1 2 3 4 5 | |
> sort(c(2, 1, 3, 5, 4), decreasing=TRUE) | |
[1] 5 4 3 2 1 | |
#In R, x[-n] returns a copy of x with the nth element removed. | |
> seq(1,10,3) | |
[1] 1 4 7 10 | |
#This is similar to range(a, b, n) in Python, except | |
#Python uses half-open intervals and so the 10 would | |
#not be included in this example. The step size | |
#argument n defaults to 1 in both R and Python. | |
#a:b is an abbreviation for seq(a, b, 1). | |
> 1:5 | |
[1] 1 2 3 4 5 | |
> seq(1,5,length=7) | |
[1] 1.000000 1.666667 2.333333 3.000000 3.666667 4.333333 5.000000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment