March, 2018
j <- list(names='Joe', salary = 55000, union = TRUE)
j
has a list with length 3. It has three different data object, character vector, numeric vector, and logical vector.names
, salary
, and union
.jn <- list('Joe', 55000, TRUE)
jini <- vector(mode = 'list', length = 10)
j$salary
## [1] 55000
j[["salary"]]
## [1] 55000
j[[2]]
## [1] 55000
j$history <- 1:10 j[[1]] <- "Yoon" j
## $names ## [1] "Yoon" ## ## $salary ## [1] 55000 ## ## $union ## [1] TRUE ## ## $history ## [1] 1 2 3 4 5 6 7 8 9 10
x <- c(5, 12, 13, 12) xf <- factor(x) xf
## [1] 5 12 13 12 ## Levels: 5 12 13
str(xf)
## Factor w/ 3 levels "5","12","13": 1 2 3 2
unclass(xf)
## [1] 1 2 3 2 ## attr(,"levels") ## [1] "5" "12" "13"
as.numeric(xf)
## [1] 1 2 3 2
xff <- factor(x, levels = c(5, 12, 13, 88)) xff
## [1] 5 12 13 12 ## Levels: 5 12 13 88
x1 <- c(4,2,3,3,2,2) table(x1)
## x1 ## 2 3 4 ## 3 2 1
x2 <- c("a","b","a","a","b","b") table(x2)
## x2 ## a b ## 3 3
x3 = data.frame(x1 = x1, x2 = x2) table(x3)
## x2 ## x1 a b ## 2 0 3 ## 3 2 0 ## 4 1 0
rbind
and cbind
A = data.frame(x1 = rep(0,2), x2 = rep('b',2)) B = data.frame(x3 = rep(1,2), x2 = rep('d',2)) AB = cbind(A,B) head(AB)
a = 0L a[2] = 1 typeof(a[1])
## [1] "double"
a = matrix(1:10,5,2) b = a[,-1] class(b)
## [1] "integer"
a = matrix(1:10,5,2) b = a[,-1, drop = FALSE] class(b)
## [1] "matrix"
c()
a = matrix(1:10,5,2) b = c(a) str(b)
## int [1:10] 1 2 3 4 5 6 7 8 9 10
as.factor()
a = c("tommy", "jimmy", "jane") b = as.factor(a) b
## [1] tommy jimmy jane ## Levels: jane jimmy tommy
# factor -> integer vector c(b)
## [1] 3 2 1
unlist()
a = list() for (i in 1:5) a[[i]] = i b = unlist(a)
as.matrix()
(=column vector)a = 1:3 b = as.matrix(a) b
## [,1] ## [1,] 1 ## [2,] 2 ## [3,] 3
as.data.frame()
a = matrix(1:10,5,2) b = as.data.frame(a) str(b)
## 'data.frame': 5 obs. of 2 variables: ## $ V1: int 1 2 3 4 5 ## $ V2: int 6 7 8 9 10
unclass()
a = matrix(1:10,5,2) b = as.data.frame(a) b = unclass(b) class(b)
## [1] "list"
as.character()
as.numeric()
= as.double()