WEEK 5:
A)Implement R Script to perform various operations on matrices
one <- matrix(1:10,nrow=10)
one
two <- matrix(51:60,nrow=2)
three <-matrix(61:70,nrow=2)
two
three
two+three
mat <- matrix(5:10,nrow=2,ncol=3,byrow=TRUE)
mat
mat2 <- matrix(1:9,nrow=3,dimnames=list(c("r1","r2","r3"),c("c1","c2","c3")))
mat2
mat2[2,2]
mat2[,1]
mat2[2,]
dim(mat2)
OUTPUT:
> matrix(
+ c(1,2,3,4,5,6,7,8),
+ nrow = 4,
+ ncol = 2,
+ byrow = FALSE )
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8
> X <- c(1, 4, 5, 2, 6, 7)
> print('using c function')
[1] "using c function"
> print(X)
[1] 1 4 5 2 6 7
> Y <- seq(1, 10, length.out = 5)
> print('using seq() function')
[1] "using seq() function"
> print(Y)
[1] 1.00 3.25 5.50 7.75 10.00
> list = c(2, 4, 4, 4, 5, 5, 7, 9)
> print(sum(list))
[1] 40
> print(mean(list))
[1] 5
> one <- matrix(1:10,nrow=10)
>one
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5
[6,] 6
[7,] 7
[8,] 8
[9,] 9
[10,] 10
> two <- matrix(51:60,nrow=2)
> three <-matrix(61:70,nrow=2)
> two
[,1] [,2] [,3] [,4] [,5]
[1,] 51 53 55 57 59
[2,] 52 54 56 58 60
> three
[,1] [,2] [,3] [,4] [,5]
[1,] 61 63 65 67 69
[2,] 62 64 66 68 70
> two+three
[,1] [,2] [,3] [,4] [,5]
[1,] 112 116 120 124 128
[2,] 114 118 122 126 130
> mat <- matrix(5:10,nrow=2,ncol=3,byrow=TRUE)
> mat
[,1] [,2] [,3]
[1,] 5 6 7
[2,] 8 9 10
> mat2 <- matrix(1:9,nrow=3,dimnames=list(c("r1","r2","r3"),c("c1","c2","c3")))
> mat2
c1 c2 c3
r1 1 4 7
r2 2 5 8
r3 3 6 9
> mat2[2,2]
[1] 5
> mat2[,1]
r1 r2 r3
1 2 3
> mat2[2,]
c1 c2 c3
2 5 8
> dim(mat2)
[1] 3 3
B)Implement R Script to extract the data from dataframes
emp.data <- data.frame(
emp_id = c (1:5),
emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
salary = c(623.3,515.2,611.0,729.0,843.25),
start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)
# Print the data frame.
print(emp.data)
str(emp.data)
summary(emp.data)
result <- data.frame(emp.data$emp_name,emp.data$salary)
print(result)
Output:
> print(emp.data)
emp_id emp_name salary start_date
1 1 Rick 623.30 2012-01-01
2 2 Dan 515.20 2013-09-23
3 3 Michelle 611.00 2014-11-15
4 4 Ryan 729.00 2014-05-11
5 5 Gary 843.25 2015-03-27
> str(emp.data)
'data.frame': 5 obs. of 4 variables:
$ emp_id : int 1 2 3 4 5
$ emp_name : chr "Rick" "Dan" "Michelle" "Ryan" ...
$ salary : num 623 515 611 729 843
$ start_date: Date, format: "2012-01-01" "2013-09-23" ...
> summary(emp.data)
emp_id emp_name salary start_date
Min. :1 Length:5 Min. :515.2 Min. :2012-01-01
1st Qu.:2 Class :character 1st Qu.:611.0 1st Qu.:2013-09-23
Median :3 Mode :character Median :623.3 Median :2014-05-11
Mean :3 Mean :664.4 Mean :2014-01-14
3rd Qu.:4 3rd Qu.:729.0 3rd Qu.:2014-11-15
Max. :5 Max. :843.2 Max. :2015-03-27
> result <- data.frame(emp.data$emp_name,emp.data$salary)
> print(result)
emp.data.emp_name emp.data.salary
1 Rick 623.30
2 Dan 515.20
3 Michelle 611.00
4 Ryan 729.00
5 Gary 843.25
C)Write R script to display file contents
file.show("pradeep.txt")
file.show("pradeep.txt")
file.show("pradeep.txt")
file.remove("pradeep.txt")
output:
TRUE
TRUE
TRUE
TRUE
D)Write R script to copy file contents from one file to another
1:Create a new directory
dir.create("newdir")
newDirPath <- "newdir"
2: Create a new file
files <- c("data.txt")
file.create(files)
newFilePath <- "data.txt"
3: Copy a file from one folder to another
dir.create("newdir")
newDirPath <- "newdir"
files <- c("data.txt")
file.create(files)
newFilePath <- "info.txt"
file.copy(newFilePath, newDirPath)
Output:
[1] TRUE
[1] TRUE