Question No. 1

Ans. to the question no. 1 (a)

given_string <- "ATCGATCGATCG-ATCGAT-CGATC-GATCGAT-CGATCG-ATCGATCG-CGATCG"
library(stringi)
## Warning: package 'stringi' was built under R version 4.3.2
gene_seqs <- unlist(strsplit(given_string, "-"))
print(gene_seqs)
## [1] "ATCGATCGATCG" "ATCGAT"       "CGATC"        "GATCGAT"      "CGATCG"      
## [6] "ATCGATCG"     "CGATCG"

Ans. to the question no. 1 (b)

# Initialize a variable to count occurrences
total_occurrences <- 0

for (i in seq_along(gene_seqs)) {
  rev_string <- stri_reverse(gene_seqs[i])
  cat("Reversed String:", rev_string, "\n")
  print(rev_string)  # Explicit print statement
}
## Reversed String: GCTAGCTAGCTA 
## [1] "GCTAGCTAGCTA"
## Reversed String: TAGCTA 
## [1] "TAGCTA"
## Reversed String: CTAGC 
## [1] "CTAGC"
## Reversed String: TAGCTAG 
## [1] "TAGCTAG"
## Reversed String: GCTAGC 
## [1] "GCTAGC"
## Reversed String: GCTAGCTA 
## [1] "GCTAGCTA"
## Reversed String: GCTAGC 
## [1] "GCTAGC"

Ans. to the question no. 1 (c)

string_length <- nchar(gene_seqs[i])
cat("Length of String:", string_length, "\n")
## Length of String: 6
print(string_length)
## [1] 6

Ans. to the question no. 1 (d)

if (string_length > 7) {
    cat("Successful\n")
  } else {
    cat("Error\n")
  }
## Error

Ans. to the question no. 1 (e)

pattern_locations <- gregexpr("GATC", gene_seqs[i])
  pattern_locations <- unlist(pattern_locations)
  
 # Check if pattern is found in the string
if (any(pattern_locations >= 0)) {
  cat("Pattern 'GATC' occurrences in string", i, ":", pattern_locations, "\n")
  total_occurrences <- total_occurrences + sum(pattern_locations >= 0)
} else {
  cat("Pattern 'GATC' not found in string", i, "\n")
}
## Pattern 'GATC' occurrences in string 7 : 2

Question No. 2

Ans. to the question no. 2 (a)

# Create the data frame
exam_score <- data.frame(
  ID = c(1, 2, 3, 4, 5),
  Name = c("Alice", "Bob", "David", "John", "Jenny"),
  Age = c(20, 25, 30, 22, 18),
  Score = c(100, 78, 90, 55, 81)
)

# Add 2 new rows
new_rows <- data.frame(
  ID = c(6, 7),
  Name = c("Eva", "Frank"),
  Age = c(28, 35),
  Score = c(85, 92)
)

exam_score <- rbind(exam_score, new_rows)

# Add a new column "Income"
exam_score$Income <- c(50000, 60000, 75000, 48000, 55000, 70000, 80000)

# Show the updated data frame
print(exam_score)
##   ID  Name Age Score Income
## 1  1 Alice  20   100  50000
## 2  2   Bob  25    78  60000
## 3  3 David  30    90  75000
## 4  4  John  22    55  48000
## 5  5 Jenny  18    81  55000
## 6  6   Eva  28    85  70000
## 7  7 Frank  35    92  80000

Ans. to the question no. 2 (b)

age<-exam_score[,3]
max(age)
## [1] 35
min(age)
## [1] 18
median(age)
## [1] 25
sum(age)
## [1] 178
mean(age)
## [1] 25.42857
sd(age)
## [1] 5.99603
var(age)
## [1] 35.95238
quantile(age, probs = 0.25)
## 25% 
##  21
score<-exam_score[,4]
max(score)
## [1] 100
min(score)
## [1] 55
median(score)
## [1] 85
sum(score)
## [1] 581
mean(score)
## [1] 83
sd(score)
## [1] 14.3527
var(score)
## [1] 206
quantile(score, probs = 0.25)
##  25% 
## 79.5
income<-exam_score[,5]
max(income)
## [1] 80000
min(income)
## [1] 48000
median(income)
## [1] 60000
sum(income)
## [1] 438000
mean(income)
## [1] 62571.43
sd(income)
## [1] 12567.91
var(income)
## [1] 157952381
quantile(income, probs = 0.25)
##   25% 
## 52500

Ans. to the question no. 2 (c)

cor(age,score)
## [1] 0.2788777
cor(age,income)
## [1] 0.9251132
cor(score,income)
## [1] 0.4444236

Ans. to the question no. 2 (d)

output<-exam_score[exam_score$Score>=80,]
output
##   ID  Name Age Score Income
## 1  1 Alice  20   100  50000
## 3  3 David  30    90  75000
## 5  5 Jenny  18    81  55000
## 6  6   Eva  28    85  70000
## 7  7 Frank  35    92  80000

Ans. to the question no. 2 (e)

age_range<-exam_score[exam_score$Age>=20 & exam_score$Age<=30,]
age_range
##   ID  Name Age Score Income
## 1  1 Alice  20   100  50000
## 2  2   Bob  25    78  60000
## 3  3 David  30    90  75000
## 4  4  John  22    55  48000
## 6  6   Eva  28    85  70000