Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

R Basic Code

The document discusses different types of vectors in R including numeric, character, and logical vectors. It also covers matrix operations and functions for calculating values of vectors like max, min, mean. The document demonstrates how to create and manipulate matrices and data frames. It provides an example of plotting data from the built-in cars dataset.

Uploaded by

kmg031125
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

R Basic Code

The document discusses different types of vectors in R including numeric, character, and logical vectors. It also covers matrix operations and functions for calculating values of vectors like max, min, mean. The document demonstrates how to create and manipulate matrices and data frames. It provides an example of plotting data from the built-in cars dataset.

Uploaded by

kmg031125
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#### 1.

Vector ####
c(1,2,3)
c("a","hellow","b")
c(1,-1,"a")

c(1:10)
seq(from=1,to=5,by=0.5)
rep(x=5,time=3)

#### 2. Vector Operations ####


1+1
5-2
2*3
6/2
5^2 # 5*5
2^-1
sqrt(5) # root 5
5^(1/2)

pi # pi
exp(2) # exponential

#### 3. Function of Vector ####


x <- c(5,1,3,7,2)
x

max(x)
min(x)
sum(x)
mean(x)
median(x)
range(x)
sort(x)

# function1 : y = k+1
my_fun <- function(k){
y = k+1
return(y)
}
my_fun(k=3)

#### 4. Types of Vectors ####

# numeric
12
class(12)

# character
"a"
class("a")
"12"
class("12")

a <- c(1,2,"b")
class(a)

#### 5. Matrix ####


matrix(c(1,2,3,4,5,6),ncol=2,byrow=FALSE)
matrix(c(1,2,3,4,5,6),ncol=2,byrow=TRUE)

mat <- matrix(c(1,2,3,4,5,6),ncol=2,byrow=F)


colnames(mat) <- c("c1","c2")
rownames(mat) <- c("r1","r2","r3")
mat

#### 6. Calculation of Matrix ####


A_mat <- matrix(c(1,2,3,4),ncol=2,byrow=F)
B_mat <- matrix(c(5,7,2,1),ncol=2,byrow=F)
A_mat
B_mat

A_mat[1,1]
A_mat[,1]
A_mat[1,]

A_mat+2
A_mat*2

A_mat + B_mat
A_mat - B_mat
A_mat*B_mat
A_mat/B_mat

A_mat%*%B_mat

matrix(c("2",1,2,3),ncol=2,byrow=F)

#### 6. Data frame ####


x1 <- c(1,2,3)
x2 <- c("a","b","c")
dat <- data.frame(c1=x1,c2=x2)
dat
dat$c3 <- c(5,6,7)
dat

dat[,1]
dat$c1

dat[1,]
dat[,-1]
dat[-1,]
#### 7. Graphic ####
cars
names(cars)
dim(cars)
head(cars)
cars$speed
cars$dist

plot(x=cars$speed,y=cars$dist)
plot(x=cars$speed,y=cars$dist,cex=3)
plot(x=cars$speed,y=cars$dist,cex=2,pch=16,col="red")
plot(x=cars$speed,y=cars$dist,cex=2,pch=16,col="red",cex.lab=1.5,xlab="Speed(x)",yl
ab="Dist(y)")
plot(x=cars$speed,y=cars$dist,cex=2,pch=16,col="red",cex.lab=1,xlab="Speed(x)",ylab
="Dist(y)"
,main="The realtionship of Dist and Speed")

You might also like