x <- 10
y <- 20
sum <- x+y
my_vector <- c(1,2,3,4,5)
my_vector[1]
## [1] 1
my_vector[5]
## [1] 5
10)Create a new data frame called my_data_frame and assign it the following values: name | age —– | —- Alice | 25 Bob | 30 Charlie | 35 11) Print the value of the first row of my_data_frame to the console. 12) Print the value of the age column of my_data_frame to the console.
my_data_frame <- data.frame(name =c("Alice","Bob","Charlie"), Age =c(25,30,35))
my_data_frame
## name Age
## 1 Alice 25
## 2 Bob 30
## 3 Charlie 35
my_data_frame[1,]
## name Age
## 1 Alice 25
my_data_frame[,2]
## [1] 25 30 35
#Variable and Data Types
num1<-10
num2<-5.5
result<-0
#Arithmetic Operations
sum_result <- num1+num2
diff_result <- num1-num2
prod_result <- num1*num2
div_result <- num1/num2
#Conditional Statements
if (num1 > num2) {print ("num1 is greater")} else {print ("num2 is greater or equal")}
## [1] "num1 is greater"
#Loops
for(i in 1:5){print (i^2)}
## [1] 1
## [1] 4
## [1] 9
## [1] 16
## [1] 25
#Functions
power_of_two <- function(x) {x^2}
square_function <- function(x) {return(x^2)}
print (square_function(num1))
## [1] 100
1.What are the different data types in R?
1.Numeric:Representing real numbers, integer and floating-point numbers
Example: M <- 5 #integer, N <- 3.55 #floating point
2.Integer:Specifically for integer values Example: A <- 50L #L
denotes an integer
3.Character:Representing text or string data Example: text <- “hello,
sir”
4.Logical: Representing either ‘true’, or ‘false’ Example: I have blue
color pen_true <- TRUE
5.Complex:Represents complex numbers with real and imaginary parts Example:'1+2i’
2.What are the different data structures in R?
1.Vectors:These are the one dimensional arrays that have same data type
Example:c(4,8,9) c(“cat,
dog”)
2.Matrices:Two-dimensional arrays with rows and columns. Created using
the matrix() function
3.Data Frames:Two-dimensional tabular data structures with rows and
columns. Similar to a matrix, but columns can have different data types.
Created using the data.frame() function.
4.Lists: Ordered collections of objects that can be of different data
types. Created using the list() function.
3.How do you create and manipulate vectors in R?
Creating Vectors:
### Numeric Vector numeric vector <- c(2,6,7)
### Character vector character_vector <- c(“Ram”, “Sita”,
“Roohi”)
#### Logical vector logical_vector <- c(TRUE, FALSE, TRUE)
Manipulating vectors:
### Accessing Elements first_element <- numeric_vector[1]
### Arithmetic operations result_vector <- numeric_vector +
c(56,70,90)
4.How do you create and manipulate matrices in R?
### Creating a Matrix: matrix_example <- matrix(1:9, nrow = 1, ncol =
4)
### Accessing Elements: element_at_1_2 <- matrix_example[1, 2]
### Modifying Elements: matrix_example[2, 3] <- 8
### Matrix Operations: matrix_sum <- matrix_example + 2
5.How do you create and manipulate data frames in R?
### Creating data frames:
### Creating a data frame with columns ‘Name’, ‘Age’, and ‘Score’ df
<- data.frame(Name = c(“Alice”, “Bob”, “Charlie”),Age = c(25, 30,
22), Score = c(95, 80, 92))
### Manipulating data frames:
#### View the entire data frame View(df)
#### View the structure of the data frame str(df)
#### Accessing the ‘Name’ column names_column <- df$Name
#### Accessing the first row first_row <- df[1, ]
#### Summary statistics for numeric columns summary(df) # PART D
data("airquality")
View(airquality)
str(airquality)
## 'data.frame': 153 obs. of 6 variables:
## $ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
## $ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
## $ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
## $ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
## $ Month : int 5 5 5 5 5 5 5 5 5 5 ...
## $ Day : int 1 2 3 4 5 6 7 8 9 10 ...
As seen in the dataset the dependent and independent variable are: dependent: Ozone Independent:wind *The dataset consist of N/A values for calucations I have ignored these values and did the regression analysis.
ozone <- airquality$Ozone
wind <- airquality$Wind
model1 <- lm(ozone~wind)
model1
##
## Call:
## lm(formula = ozone ~ wind)
##
## Coefficients:
## (Intercept) wind
## 96.873 -5.551
For every 1 mph increase in wind speed, the predicted value of the Ozone decreases by 5.551 ppb, assuming all other variables are held constant.
predict(model1,newdata= data.frame(wind=5.5))
## 1
## 66.34282
For every 5.5 mph increase in wind speed, the predicted value of the Ozone decreases by 66.34282 ppb, assuming all other variables are held constant.
predict(model1,newdata= data.frame(wind=10))
## 1
## 41.36367
For every 10 mph increase in wind speed, the predicted value of the Ozone decreases by 41.36367 ppb, assuming all other variables are held constant.
plot <- plot(ozone,wind,type = "p",main="change in ozone with the change in wind",sub ="General Information",xlab="PPB",ylab="MPH",col= "firebrick")
abline(model1)
variables to be used ozone,wind,temperature
temperature <- airquality$Temp
model2 <- lm(ozone~wind+temperature)
model2
##
## Call:
## lm(formula = ozone ~ wind + temperature)
##
## Coefficients:
## (Intercept) wind temperature
## -71.033 -3.055 1.840
The model2 predicts that, on average, when both wind speed and temperature are zero, the ozone level is estimated to be -71.033 units. 1.For every 1mph increase in wind speed, the model predicts a decrease in the ozone level of 3.055 ppb, holding temperature constant. 2.For every 1mph increase in temperature, the model predicts an increase in the ozone level of 1.840 ppb, holding wind speed constant.