-
Notifications
You must be signed in to change notification settings - Fork 0
/
Revision work.R
137 lines (105 loc) · 2.83 KB
/
Revision work.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# finding the factors of a number
find_factors <- function(x){
# creating the array from 1:x
y <- 1:x
# filtering the appropriate values where
# the remainder is zero
y[x %% y == 0 ]
}
# creating a function for identifying prime numbers
is_prime <- function(x){
factors = find_factors(x)
n = length(factors)
if (n == 2){
return(TRUE)
}else{
return(FALSE)
}
}
#
square_free<- function(n){
# getting the factors of the number
factors = find_factors(n)
# getting the square root of the factors
sqrt_factors = sqrt(factors)
# picking only the integer square roots
int_factors = sqrt_factors[sqrt_factors %% 1 == 0]
# creating a vectors which shows whether the number
# is prime or not
primes = sapply(int_factors, is_prime)
# checking if there is a prime number in the square roots
if(any(primes==TRUE)){
return (TRUE)
}else{
return(FALSE)
}
}
# example using the function
square_free(30)
# While loop
# in this case , a certain action is performed
# as long as a condition is true
# initialize a variable
i <- 1
while(i <= 10){
print("This is a good day!")
i = i+1
}
# for a loop, it will iterate through all the elements
students <- c("elvis", "brian", "julliet")
for (student in students){
print(student)
}
#
# ggplot2 is library
# first, you need to install ggplot2 since it's an
# external library/package/module
#installing the package
install.packages("ggplot2")
# once you have a package, you can call it or tell
# the computer you would like to use it
# you do this using the library function
library(ggplot2)
# view the first six observations in data
head(data)
# names of the columns
names(data)
# for me to use the dataframe, it's a good idea to
# attach it..to avoid using dollar signs
attach(data)
# create a scatterplot of Sepal.Length against
# Sepal.Width
#without ggplot2
plot(Sepal.Length~Sepal.Width,
main="Sepal Length vs Sepal Width",
xlab="Sepal Width",
ylab="Sepal Length")
# using ggplot2
# ggplot2: grammar of graphics
# plot/graph = data + aesthetics + geometry
ggplot(data, aes(x=Sepal.Width, y=Sepal.Length))+
geom_point()+xlab("Sepal Width")+
ylab("Sepal Length")+ ggtitle("Sepal Length vs Sepal Width")
#
# histogram question
# creating some random numbers
random_sample <- sample(50000:120000, 60)
random_sample
breaks <- seq(20000, 120000, by=10000)
# creating a vector that stores colors
colors <- rep(c("red","blue"), 5)
colors
hist(random_sample, breaks = breaks, col=colors, xlab="Numbers")
# solving simultaneous
# equations
# creating the matrix
m <- rbind(
c(10, 4, 1),
c(5, 10, 2),
c(15, 3, 4)
)
m
# create solution vector
v <- c(345, 450, 490)
# solving the equation
solve(m, v)