2. Using list, Data frame and functions in R

Sahil Sahani
0

 


2. Using list, Data frame and functions in R



2A) write an R program to to manage data and exhibit operation on it using list data structure.

# Create a list with information about people
peopleList <- list(
  names = c("Alice", "Bob", "Charlie"),
  ages = c(25, 30, 22),
  scores = c(95, 89, 75)
)

# Display information from the list
cat("Initial List:\n")
cat("Names: ", peopleList$names, "\n")
cat("Ages: ", peopleList$ages, "\n")
cat("Scores: ", peopleList$scores, "\n")

# Add a new person to the list
newPerson <- list(name = "David", age = 28, score = 80)
peopleList$names <- c(peopleList$names, newPerson$name)
peopleList$ages <- c(peopleList$ages, newPerson$age)
peopleList$scores <- c(peopleList$scores, newPerson$score)

# Display the list after adding a new person
cat("\nList after adding David:\n")
cat("Names: ", peopleList$names, "\n")
cat("Ages: ", peopleList$ages, "\n")
cat("Scores: ", peopleList$scores, "\n")

# Calculate and display the average age
averageAge <- mean(peopleList$ages)
cat("\nAverage Age: ", averageAge, "\n")

OUTPUT :

Initial List:
Names:  Alice Bob Charlie 
Ages:  25 30 22 
Scores:  95 89 75 

List after adding David:
Names:  Alice Bob Charlie David 
Ages:  25 30 22 28 
Scores: 95 89 75 80 
Average Age:  26.25 
    

2B) Write an R program to manage data and exhibit operation on it using Data Frames..

# Create a data frame with information about people
peopleData <- data.frame(
  Name = c("Alice", "Bob", "Charlie"),
  Age = c(25, 30, 22),
  Score = c(95, 89, 75)
)

# Display the data frame
cat("Initial Data Frame:\n")
print(peopleData)

# Add a new person to the data frame
newPerson <- data.frame(Name = "David", Age = 28, Score = 80)
peopleData <- rbind(peopleData, newPerson)

# Display the data frame after adding a new person
cat("\nData Frame after adding David:\n")
print(peopleData)

# Calculate and display the average age
averageAge <- mean(peopleData$Age)
cat("\nAverage Age: ", averageAge, "\n")

OUTPUT :

Initial Data Frame:
Name Age Score
1   Alice  25    95
2     Bob  30    89
3 Charlie  22    75

Data Frame after adding David:
Name Age Score
1   Alice  25    95
2     Bob  30    89
3 Charlie  22    75
4   David  28    80
Average Age:  26.25 

    

2C) write an R program to demonstrate the use of i) user-defined functions. ii) built-in numeric function ,character function etc..

# User-defined function to calculate the square of a number
square <- function(x) {
  return(x^2)
}

# Built-in function to calculate the mean of a vector
numbers <- c(2, 4, 6, 8, 10)
meanValue <- mean(numbers)

# Character function to concatenate strings
greeting <- paste("Hello", "World!")

# Display results
cat("i) Using User-defined Function:\n")
resultSquare <- square(5)
cat("Square of 5:", resultSquare, "\n\n")

cat("ii) Using Built-in Function:\n")
cat("Mean of the numbers:", meanValue, "\n\n")

cat("iii) Using Character Function:\n")
cat("Greeting:", greeting, "\n")

OUTPUT :

i) Using User-defined Function:
Square of 5: 25 

ii) Using Built-in Function:
Mean of the numbers: 6 

iii) Using Character Function:
Greeting: Hello World!
    

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)