0% found this document useful (0 votes)
2 views5 pages

HW 1

The document contains R programming code snippets demonstrating various operations such as creating vectors and matrices, performing arithmetic operations, calculating lengths, and generating random numbers. It also includes examples of plotting data and manipulating matrix dimensions. Additionally, it shows how to handle data structures and perform statistical calculations like mean, variance, and standard deviation.

Uploaded by

hubertkuo418
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

HW 1

The document contains R programming code snippets demonstrating various operations such as creating vectors and matrices, performing arithmetic operations, calculating lengths, and generating random numbers. It also includes examples of plotting data and manipulating matrix dimensions. Additionally, it shows how to handle data structures and perform statistical calculations like mean, variance, and standard deviation.

Uploaded by

hubertkuo418
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

hw1

2024-09-12
x <‐ c(1, 3, 2, 5)
x

## [1] 1 3 2 5

x = c(1, 6, 2)
x

## [1] 1 6 2

y = c(1, 4, 3)

length(x)

## [1] 3

length(y)

## [1] 3

x + y

## [1] 2 10 5

ls()

## [1] "x" "y"

rm(x, y)

ls()

## character(0)

rm( list = ls())

x <‐ matrix(c(1, 2, 3, 4), nrow =2, ncol = 2)


x

## [,1] [,2]
## [1,] 1 3
## [2,] 2 4

x <‐ matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE)


x
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4

sqrt(x)

## [,1] [,2]
## [1,] 1.000000 1.414214
## [2,] 1.732051 2.000000

x^2

## [,1] [,2]
## [1,] 1 4
## [2,] 9 16

x <‐ rnorm(50)
y <‐ x+rnorm (50, mean = 50, sd = .1)
cor (x,y)

## [1] 0.9952086

set.seed(1303)
x <‐ rnorm (50)

set.seed (3)
y <‐ rnorm (100)
mean (y)

## [1] 0.01103557

var(y)

## [1] 0.7328675

sqrt(var(y))

## [1] 0.8560768

sd(y)

## [1] 0.8560768

x <‐ rnorm(100)
y <‐ rnorm(100)
plot(x, y)
plot(x, y, xlab = "this is the x‐ axis ", ylab = "this is the y‐ axis ",
main = "Plot of X vs Y")
x <‐ seq (1,10)
x

## [1] 1 2 3 4 5 6 7 8 9 10

x <‐ 1:10
x

## [1] 1 2 3 4 5 6 7 8 9 10

x <‐ seq (‐pi, pi, length = 50)

rm( list = ls())

A <‐ matrix (1:17, 4, 4)

## Warning in matrix(1:17, 4, 4): data length [17] is not a sub‐multipl


e or
## multiple of the number of rows [4]

## [,1] [,2] [,3] [,4]


## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16

A[2,3]

## [1] 10

A[c(1, 3), c(2, 4)]

## [,1] [,2]
## [1,] 5 13
## [2,] 7 15

A[1:3, 2:4]

## [,1] [,2] [,3]


## [1,] 5 9 13
## [2,] 6 10 14
## [3,] 7 11 15

A[1:2,]

## [,1] [,2] [,3] [,4]


## [1,] 1 5 9 13
## [2,] 2 6 10 14

A[, 1:2]
## [,1] [,2]
## [1,] 1 5
## [2,] 2 6
## [3,] 3 7
## [4,] 4 8

A[1,]

## [1] 1 5 9 13

A[‐c(1,3),]

## [,1] [,2] [,3] [,4]


## [1,] 2 6 10 14
## [2,] 4 8 12 16

A[‐c(1,3), ‐c(1, 3, 4)]

## [1] 6 8

dim (A)

## [1] 4 4

You might also like