Skip to main content

List of R Programs

List of Programs for practise of R Programming
  1. Write a program to calculate the sum of digits of a given number.
  2. Create a function to compute the factorial of a number using both iterative and recursive methods.
  3. Implement a program to convert a number from decimal to binary, octal, and hexadecimal.
  4. Write a function to count the number of vowels and consonants in a given string.
  5. Create a program that reverses a given string without using built-in functions.
  6. Write a program to merge two sorted lists into a single sorted list.
  7. Create a function to find the largest and smallest numbers in a list.
  8. Develop a program to count the frequency of each character in a string.
  9. Write a program to convert a binary number (as a string) to its decimal equivalent.
  10. Create a function to convert temperatures between Celsius, Fahrenheit, and Kelvin.
  11. Implement a program to find the sum of even and odd numbers from a list.
  12. Develop a program to sort a list of tuples based on the second element in each tuple.
  13. Create a program to generate a random password of specified length with letters and digits.
  14. Write a function to remove duplicate elements from a list while preserving the order.
  15. Implement a program to find common elements between two lists.
  16. Create a function to find all prime factors of a given number.
  17. Develop a program to find the Greatest Common Divisor (GCD) of two numbers using the Euclidean algorithm.
  18. Implement a program to find the n_th largest number in a list.
  19. Write a function to remove all whitespace characters from a given string.
  20. Implement a program to transpose a given matrix.

R Programs for 20 Problems - Professor Gunupudi

1. Program to calculate the sum of digits of a given number


sum_of_digits <- function(n) {
  digits <- as.numeric(unlist(strsplit(as.character(n), "")))
  return(sum(digits))
}
# Example
sum_of_digits(1234)  # Output: 10

2. Function to compute the factorial using iterative and recursive methods


# Iterative method
factorial_iter <- function(n) {
  result <- 1
  for (i in 1:n) {
    result <- result * i
  }
  return(result)
}

# Recursive method
factorial_rec <- function(n) {
  if (n <= 1) return(1)
  return(n * factorial_rec(n - 1))
}

# Example
factorial_iter(5)  # Output: 120
factorial_rec(5)   # Output: 120

3. Convert a number from decimal to binary, octal, and hexadecimal


convert_number <- function(n) {
  binary <- paste(rev(as.integer(intToBits(n))), collapse = "")
  binary <- sub("^0+", "", binary)
  octal <- as.character(as.octmode(n))
  hex <- toupper(as.character(as.hexmode(n)))
  return(list(Binary = binary, Octal = octal, Hexadecimal = hex))
}

# Example
convert_number(255)

4. Count number of vowels and consonants in a string


count_vowels_consonants <- function(text) {
  text <- tolower(gsub("[^a-z]", "", text))
  chars <- unlist(strsplit(text, ""))
  vowels <- sum(chars %in% c('a', 'e', 'i', 'o', 'u'))
  consonants <- length(chars) - vowels
  return(list(Vowels = vowels, Consonants = consonants))
}

# Example
count_vowels_consonants("Hello World")

5. Reverse a string without using built-in reverse function


reverse_string <- function(s) {
  chars <- strsplit(s, "")[[1]]
  reversed <- chars[length(chars):1]
  return(paste(reversed, collapse = ""))
}

# Example
reverse_string("OpenAI")

6. Merge two sorted lists into a single sorted list


merge_sorted_lists <- function(list1, list2) {
  merged <- sort(c(list1, list2))
  return(merged)
}

# Example
merge_sorted_lists(c(1, 3, 5), c(2, 4, 6))

7. Find the largest and smallest numbers in a list


find_min_max <- function(vec) {
  return(list(Smallest = min(vec), Largest = max(vec)))
}

# Example
find_min_max(c(12, 5, 8, 19, 3))

8. Count the frequency of each character in a string


char_frequency <- function(text) {
  chars <- unlist(strsplit(tolower(gsub("[^a-z]", "", text)), ""))
  return(table(chars))
}

# Example
char_frequency("Hello World")

9. Convert a binary number (string) to its decimal equivalent


binary_to_decimal <- function(bin_str) {
  digits <- as.numeric(strsplit(bin_str, "")[[1]])
  decimal <- sum(digits * 2^((length(digits)-1):0))
  return(decimal)
}

# Example
binary_to_decimal("1101")

10. Convert temperatures between Celsius, Fahrenheit, and Kelvin


convert_temperature <- function(value, from, to) {
  if (from == "C") {
    if (to == "F") return((value * 9/5) + 32)
    if (to == "K") return(value + 273.15)
  } else if (from == "F") {
    if (to == "C") return((value - 32) * 5/9)
    if (to == "K") return((value - 32) * 5/9 + 273.15)
  } else if (from == "K") {
    if (to == "C") return(value - 273.15)
    if (to == "F") return((value - 273.15) * 9/5 + 32)
  }
  return(NA)
}

# Example
convert_temperature(100, "C", "F")

11. Find the sum of even and odd numbers from a list


sum_even_odd <- function(numbers) {
  even_sum <- sum(numbers[numbers %% 2 == 0])
  odd_sum <- sum(numbers[numbers %% 2 != 0])
  return(list(Even_Sum = even_sum, Odd_Sum = odd_sum))
}

# Example
sum_even_odd(c(1, 2, 3, 4, 5, 6))

12. Sort a list of tuples based on the second element


sort_tuples_by_second <- function(tuples) {
  sorted <- tuples[order(sapply(tuples, function(x) x[2]))]
  return(sorted)
}

# Example
list_of_tuples <- list(c("a", 3), c("b", 1), c("c", 2))
sort_tuples_by_second(list_of_tuples)

13. Generate a random password of specified length with letters and digits


generate_password <- function(length) {
  chars <- c(letters, LETTERS, 0:9)
  paste0(sample(chars, length, replace = TRUE), collapse = "")
}

# Example
generate_password(10)

14. Remove duplicate elements from a list while preserving the order


remove_duplicates <- function(vec) {
  unique_vec <- vec[!duplicated(vec)]
  return(unique_vec)
}

# Example
remove_duplicates(c(1, 2, 2, 3, 1, 4))

15. Find common elements between two lists


find_common_elements <- function(list1, list2) {
  return(intersect(list1, list2))
}

# Example
find_common_elements(c(1, 2, 3, 4), c(3, 4, 5, 6))

16. Find all prime factors of a given number


prime_factors <- function(n) {
  factors <- c()
  i <- 2
  while (i <= n) {
    if (n %% i == 0) {
      factors <- c(factors, i)
      n <- n / i
    } else {
      i <- i + 1
    }
  }
  return(factors)
}

# Example
prime_factors(84)

17. Find the Greatest Common Divisor (GCD) using the Euclidean algorithm


gcd <- function(a, b) {
  while (b != 0) {
    temp <- b
    b <- a %% b
    a <- temp
  }
  return(a)
}

# Example
gcd(48, 18)

18. Find the n-th largest number in a list


nth_largest <- function(vec, n) {
  sorted <- sort(unique(vec), decreasing = TRUE)
  if (n <= length(sorted)) {
    return(sorted[n])
  } else {
    return(NA)
  }
}

# Example
nth_largest(c(4, 1, 7, 7, 2, 9), 3)

19. Remove all whitespace characters from a given string


remove_whitespace <- function(text) {
  return(gsub("\\s+", "", text))
}

# Example
remove_whitespace("  Hello   World \n\t")

20. Transpose a given matrix


transpose_matrix <- function(mat) {
  return(t(mat))
}

# Example
m <- matrix(1:6, nrow = 2, ncol = 3)
transpose_matrix(m)

Comments

Post a Comment

Popular posts from this blog

Data Visualization

Welcome to Data Visualization Lab  B.Tech Information Technology, II Year I Semester, Section C Softwares for Installation Test Link  https://forms.gle/Fnh79CyodToWTGBj7 RStudio Link:  https://posit.co/download/rstudio-desktop/   Power BI Link https://www.microsoft.com/en-us/download/details.aspx?id=58494 Tableau Desktop https://www.tableau.com/products/desktop/download  Week-1 Experiments List https://drive.google.com/file/d/1UlRKXTY9OdK7QyVLa7H-_dmaJkimuecs/view?usp=drive_link  W3Schools -> R Programming  https://www.w3schools.com/R/ Experiments List List of Experiments 1: Programming Practise in R  - Click Here List of Experiments 2 :  Click Here List of Experiments 3 : Click Here   

Data Mining - Assignment & ELA Test-1

  Table – 1 Groceries Dataset Transaction ID Items T1 Milk, Bread, Butter, Eggs, Sugar, Tea, Coffee, Biscuit, Cheese, Yogurt T2 Bread, Butter, Jam, Eggs, Milk, Sugar, Biscuit, Tea, Coffee, Yogurt T3 Bread, Butter, Cheese, Jam, Eggs, Milk, Yogurt, Biscuit, Tea, Coffee T4 Milk, Bread, Cheese, Butter, Sugar, Eggs, Coffee, Tea, Yogurt, Biscuit T5 Butter, Bread, Jam, Milk, Sugar, Coffee, Biscuit, Eggs, Yogurt, Tea T6 Milk, Butter, Cheese, Sugar, Biscuit, Yogurt, Eggs, Bread, Coffee, Tea T7 Bread, Jam, Milk, Butter, Eggs, Biscuit, Coffee, Yogurt, Sugar, Tea T8 Butter, Cheese, Bread, Eggs, Milk, Biscuit, Sugar, Tea, Coffee, Yogurt T9 Bread, Butter, Jam, Cheese, Yogurt, Biscuit, Coffee, Tea, Eggs, Milk T10 Milk, Bread, Butter...

Tableau

TABLEAU EXPERIMENTS Consider the Dhoni Dataset  click here  solve the following experiments using Tableau Tool Here are a few experiment ideas for students to create visualizations in Tableau using the cricket dataset: Experiment 1: Runs vs. Opponent Objective:  Visualize how the player's performance in terms of runs varies against different opponents. Task:  Create a bar chart showing the total runs scored against each opposing team. Additional Insight:  Add a color gradient to highlight highest to lowest run totals. Experiment 2: Performance Over Time Objective:  Analyze the player’s performance trends over the course of the season. Task:  Develop a line chart that plots runs scored over time (by date). Additional Insight:  Include a dual-axis to plot catches and stumpings alongside runs. Experiment 3: Heatmap of Performances by Stadium Objective:  Compare the player’s performance at different stadiums. Task:  Create a heatmap to displ...