R
R seq(), list(), matrix() - 2020/01/06
jchung56
2020. 1. 6. 18:53
title: "test4"
output: html_document
knitr::opts_chunk$set(echo = TRUE)
#sequence seq(from,end,by)
seq(3,10,2)
?seq_along()
vec<-1:10
for (i in vec) {
print(i)
}
for (i in seq_along(vec)) {
print(i)
}
df<-data.frame(a=1:3,b=5:7)
df
?data.frame
for (i in seq_len(ncol(df))) {
print(i)
}
rep(1:2, times=5)
rep(1:3, each=3)
rep(1:2,each=5,times=2)
#list : (key=value, key1=value1) can store different data type, scala or vector
x<-list(name="juhee", height=152)
x
y<-list(name="seungchan",height=c(130,180,182))
y
#list() we can store another list() in list()
k=list(a=list(val=c(1,2,3,4)),b=list(val=c(1,2,3,4,5,6)))
k$a$val
k$b$val
#access data in list() by index
k[1]
#$a
#$a$val
#[1] 1 2 3 4
k[[1]]
#$val
#[1] 1 2 3 4
k$a
#Matrix can store 1 type of scala, must have the same data type in every column
matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3, dimnames = list(c("r1","r2","r3"),c("c1","c2","c3")))
# c1 c2 c3
#r1 1 4 7
#r2 2 5 8
#r3 3 6 9
m<-matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3,byrow = TRUE, dimnames = list(c("r1","r2","r3"),c("c1","c2","c3")))
# c1 c2 c3
#r1 1 2 3
#r2 4 5 6
#r3 7 8 9
m[1:2,] # m[row,column]
m[,2:3]
m[,-1]# remove the first column
?t()
#t() : transpose of a complex matrix A^*
t(m)
?solve
nrow(m)
ncol(m)
dim(m)
#to notice whether the matrix can be identity matrix
(x<-matrix(c(1,2,3,4),ncol = 2))
# [,1] [,2]
#[1,] 1 3
#[2,] 2 4
solve(x)
x %*% solve(x)
# we can alter dimension of matrix with dim()
dim(x)<-c(4,1)
x