Q1. Create a numeric vector containing the first 15 positive integers. Find: Length of the vector, Sum of elements, Mean of elements

x = 1:15 
len <- length(x)

total = 0 
for (i in x){
  total = total + i 
}
mean_val = total/ len
len 
## [1] 15
total 
## [1] 120
mean_val
## [1] 8

Q2. Create a vector containing the squares of numbers from 1 to 10.

nums = 1:10 
squares = nums * nums 
squares
##  [1]   1   4   9  16  25  36  49  64  81 100

Q3. Create a character vector of 5 programming languages and display: The first element & The last element

langs = c("R","CPP","Python", "Java","SQL")
first_lang = langs[1]
last_lang = langs[length(langs)]
first_lang
## [1] "R"
last_lang
## [1] "SQL"

Q4. Create a logical vector by checking whether numbers from 1 to 20 are even.

nums = 1:20
even_check = (nums %% 2) == 0
nums
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
even_check
##  [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
## [13] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE

Q5. Create a numeric vector and determine: Minimum value, Maximum value, Range

x = c (12,45,7,33,19)
min_val = x[1]
max_val = x[1]

for (i in x){
  if (i< min_val) min_val = i
  if (i> max_val) max_val = i 
}

range_val = max_val - min_val

min_val
## [1] 7
max_val
## [1] 45
range_val
## [1] 38

Q.6 - Create a vector of numbers from 10 to 50. Extract only the numbers greater than 30.

x = 10:50
x[x>30]
##  [1] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

Q7. Given the vector: x <- c(12, 5, 8, 20, 15, 3) Find all values that are: Greater than 10 & Less than 20

x <- c(12, 5, 8, 20, 15, 3)
x[x>10 & x< 20]
## [1] 12 15

Q8. Write a user-defined function square_cube() that: Takes a number as input, Returns its square and cube

square_cube = function(n){
  square = n * n
  cube = n*n*n
  c(square= square, cube = cube)
}

square_cube(4)
## square   cube 
##     16     64

Q9. Write a function is_even() that checks whether a number is even or odd and prints an appropriate message.

is_even = function(n){
  if (n%%2==0){
    "even"
  } else {
    "odd"
  }
}
is_even(1001)
## [1] "odd"

Q10. Create a function sum_n() that calculates the sum of integers from 1 to n. Use the function to find the sum of integers from 1 to 1,000.

sum_n = function(n){
  total = 0 
  for (i in 1:n){
    total = total +i
  }
  total
}

sum_n(1000)
## [1] 500500

Q11. Load the built-in dataset mtcars. Display: Number of rows, Number of columns, Column names

data(mtcars)
row = nrow(mtcars)
columns = ncol(mtcars)

names_cols = names(mtcars)

row
## [1] 32
columns
## [1] 11
names_cols
##  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
## [11] "carb"
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Q12. From mtcars, extract: Miles per gallon (mpg), Number of cylinders (cyl) Store them in a new data frame.

new_df = data.frame(
  mpg = mtcars$mpg,
  cyl = mtcars$cyl
)
new_df
##     mpg cyl
## 1  21.0   6
## 2  21.0   6
## 3  22.8   4
## 4  21.4   6
## 5  18.7   8
## 6  18.1   6
## 7  14.3   8
## 8  24.4   4
## 9  22.8   4
## 10 19.2   6
## 11 17.8   6
## 12 16.4   8
## 13 17.3   8
## 14 15.2   8
## 15 10.4   8
## 16 10.4   8
## 17 14.7   8
## 18 32.4   4
## 19 30.4   4
## 20 33.9   4
## 21 21.5   4
## 22 15.5   8
## 23 15.2   8
## 24 13.3   8
## 25 19.2   8
## 26 27.3   4
## 27 26.0   4
## 28 30.4   4
## 29 15.8   8
## 30 19.7   6
## 31 15.0   8
## 32 21.4   4

Q13. Find the average mileage (mpg) for cars with: 4 cylinders, 6 cylinders, 8 cylinders

avg_mpg = function(cyl_val){
  total = 0
  count = 0
  
  for (i in 1:nrow(mtcars)) {
    if (mtcars$cyl[i] == cyl_val){
      total = total + mtcars$mpg[i]
      count = count +1
    }
  }
  total/count
}

avg_mpg(4)
## [1] 26.66364
avg_mpg(6)
## [1] 19.74286
avg_mpg(8)
## [1] 15.1
#the average mileage (mpg) for cars with: 4 cylinders is 26.66, 6 cylinders is 19.74 and for 8 cylinders is 15.1

Q14. Create a new column in mtcars that converts mileage from miles per gallon to kilometers per liter.

mtcars$km_per_liter = mtcars$mpg * 0.425
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
##                   km_per_liter
## Mazda RX4               8.9250
## Mazda RX4 Wag           8.9250
## Datsun 710              9.6900
## Hornet 4 Drive          9.0950
## Hornet Sportabout       7.9475
## Valiant                 7.6925

Q15. Identify the car with: Highest mileage Lowest mileage

max_index =  1
min_index = 1

for (i in 1:nrow(mtcars)) {
  if (mtcars$mpg[i] > mtcars$mpg[max_index]) max_index = i 
  if (mtcars$mpg[i] < mtcars$mpg[min_index]) min_index = i
}

mtcars[max_index,]
##                 mpg cyl disp hp drat    wt qsec vs am gear carb km_per_liter
## Toyota Corolla 33.9   4 71.1 65 4.22 1.835 19.9  1  1    4    1      14.4075
mtcars[min_index,]
##                     mpg cyl disp  hp drat   wt  qsec vs am gear carb
## Cadillac Fleetwood 10.4   8  472 205 2.93 5.25 17.98  0  0    3    4
##                    km_per_liter
## Cadillac Fleetwood         4.42

Q16. Using the mtcars dataset, find how many cars: Have mpg greater than 20 (ii) Have horsepower (hp) greater than 150

count_mpg = 0 
count_hp = 0

for (i in 1:nrow(mtcars)){
  if (mtcars$mpg[i] > 20) count_mpg = count_mpg + 1
  if (mtcars$hp[i] > 150) count_hp = count_hp + 1
}


count_mpg
## [1] 14
count_hp
## [1] 13

Q17. Create a subset of mtcars that includes only cars with: Automatic transmission, mpg greater than 18

subset_mtcars = mtcars[mtcars$am==1 & mtcars$mpg > 18,]
subset_mtcars
##                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb km_per_liter
## Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4       8.9250
## Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4       8.9250
## Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1       9.6900
## Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1      13.7700
## Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2      12.9200
## Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1      14.4075
## Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1      11.6025
## Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2      11.0500
## Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2      12.9200
## Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6       8.3725
## Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2       9.0950

Q18. Rank cars based on mileage from highest to lowest and display the top 5.

mpg_vals = mtcars$mpg
ranked =  order(-mpg_vals)
  
mtcars[ranked[1:5],]  
##                 mpg cyl disp  hp drat    wt  qsec vs am gear carb km_per_liter
## Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1      14.4075
## Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1      13.7700
## Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2      12.9200
## Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2      12.9200
## Fiat X1-9      27.3   4 79.0  66 4.08 1.935 18.90  1  1    4    1      11.6025

Q19. Create a function summary_stats() that: Takes a numeric vector Returns mean, median, minimum, and maximum Apply this function to the mpg column of mtcars.

summary_stats =  function(x) {
  min_val = x[1]
  max_val = x[1]
  total = 0
  
  for (i in x) {
    if(i < min_val) min_val = i
    if(i > max_val) max_val = i
    total = total +i
  }
 mean_val = total/length(x)
 
 sorted_x = sort(x)
 n = length(x)
 if (n %% 2 == 1) {
   median_val = sorted_x[(n+1)/2]
 } else {
   median_val = (sorted_x[(n/2)] + sorted_x[n/2 + 1])  /2
 }

 c(mean = mean_val, median = median_val, min =min_val, max= max_val)
}


summary_stats(mtcars$mpg)
##     mean   median      min      max 
## 20.09062 19.20000 10.40000 33.90000
summary(mtcars$mpg)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   10.40   15.43   19.20   20.09   22.80   33.90

Q.20 Compute the percentage of cars that have: More than 6 cylinders