# Define variables with different data types
a <- 55
b <- "Hello, Vnr"
c <- FALSE
d <- as.POSIXct("2023-11-11 15:20:00")
# Print variables
cat("Numeric Variable:", a, "\n")
## Numeric Variable: 55
cat("Character Variable:", b, "\n")
## Character Variable: Hello, Vnr
cat("Logical Variable:", c, "\n")
## Logical Variable: FALSE
cat("Date and Time Variable:", d, "\n")
## Date and Time Variable: 1699696200
# Create data structures
vector <- c(7, 9, 3, 2, 5)
list <- list(10, "apple", TRUE)
matrix <- matrix(1:9, nrow = 3, ncol = 3)
data_frame <- data.frame(
Name = c("Ram", "Kiran", "Vikram"),
Age = c(34, 23, 28),
Score = c(45, 12, 56)
)
# Print data structures
cat("Vector Example:", vector, "\n")
## Vector Example: 7 9 3 2 5
cat("Matrix Example:\n")
## Matrix Example:
print(matrix)
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
cat("List Example:\n")
## List Example:
print(list)
## [[1]]
## [1] 10
##
## [[2]]
## [1] "apple"
##
## [[3]]
## [1] TRUE
cat("Data Frame Example:\n")
## Data Frame Example:
print(data_frame)
## Name Age Score
## 1 Ram 34 45
## 2 Kiran 23 12
## 3 Vikram 28 56
# a.Create two vectors of integers
v1 <- c(4,1,2)
v2 <- c(1,7,9)
# Add the two vectors element-wise
sum <- v1 + v2
# Print the result
print(sum)
## [1] 5 8 11
we have added 2 vectors successfully.
# b.Create a vector
vector <- c(3,6,8,1)
# Calculate the sum, mean, and product
sum <- sum(vector)
mean <- mean(vector)
product<- prod(vector)
# Print the results
print(paste("Sum:", sum))
## [1] "Sum: 18"
print(paste("Mean:", mean))
## [1] "Mean: 4.5"
print(paste("Product:", product))
## [1] "Product: 144"
we have found sum,mean and product of vector elements.
# c.Create a vector
v1<- c(2,5,8,1,9)
# Find the minimum and maximum
minimum_of_v1<- min(v1)
maximum_of_v1<- max(v1)
# Print the results
print(paste("Minimum:",minimum_of_v1))
## [1] "Minimum: 1"
print(paste("Maximum:",maximum_of_v1))
## [1] "Maximum: 9"
#d. Create a list
list <- list(
string = "Hello, vnr",
numeric = 55,
vector = c(2,3,4),
logical = TRUE
)
# Print the list
print(list)
## $string
## [1] "Hello, vnr"
##
## $numeric
## [1] 55
##
## $vector
## [1] 2 3 4
##
## $logical
## [1] TRUE
a heterogeneous list is made.
#e. Create a list with named elements
list <- list(
vector = c(1, 2, 3),
matrix = matrix(1:9, nrow = 3),
nestedlist = list(a1 = "vnr", a2 = "vjiet")
)
# Access the first and second elements of the list
element1 <- list$vector
element2<- list$matrix
# Print the accessed elements
print(element1)
## [1] 1 2 3
print(element2)
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
#f. Create a 3x5 matrix filled with zeros
my_matrix <- matrix(0, nrow = 3, ncol = 5)
# Print the matrix
print(my_matrix)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0 0 0 0 0
## [2,] 0 0 0 0 0
## [3,] 0 0 0 0 0
#g. Create a sample matrix
my_matrix <- matrix(1:12, nrow = 3)
# Access specific elements
element_1 <- my_matrix[2, 3] # 3rd column, 2nd row
element_2 <- my_matrix[3, ] # 3rd row
element_3 <- my_matrix[, 4] # 4th column
# Print the accessed elements
print(element_1)
## [1] 8
print(element_2)
## [1] 3 6 9 12
print(element_3)
## [1] 10 11 12
#h. Create vectors
name <- c("Alice", "Bob", "Charlie")
age <- c(25, 30, 35)
# Create a DataFrame
df <- data.frame(Name = name, Age = age)
# Display the DataFrame
print(df)
## Name Age
## 1 Alice 25
## 2 Bob 30
## 3 Charlie 35
#i. Create a DataFrame
df <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30))
# New data to insert
new_data <- data.frame(Name = c("Charlie", "David"), Age = c(35, 40))
# Insert new rows
df <- rbind(df, new_data)
# Display the updated DataFrame
print(df)
## Name Age
## 1 Alice 25
## 2 Bob 30
## 3 Charlie 35
## 4 David 40
#j. Create a DataFrame
df <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30))
# Add a new column
df$Salary <- c(50000, 60000)
# Display the updated DataFrame
print(df)
## Name Age Salary
## 1 Alice 25 50000
## 2 Bob 30 60000
#k. Create a DataFrame
df <- data.frame(Name = c("Alice", "Bob", "Charlie", "David"), Age = c(25, 30, 35, 40))
# Extract the first 2 rows
first_two_rows <- df[1:2, ]
# Display the extracted rows
print(first_two_rows)
## Name Age
## 1 Alice 25
## 2 Bob 30
#l. Create a DataFrame
df <- data.frame(Name = c("Charlie", "Alice", "Bob"), Age = c(35, 25, 30))
# Sort the DataFrame by the "Age" column
sorted_df <- df[order(df$Age), ]
# Display the sorted DataFrame
print(sorted_df)
## Name Age
## 2 Alice 25
## 3 Bob 30
## 1 Charlie 35
#m. Create two DataFrames
df1 <- data.frame(ID = 1:3, Name = c("Alice", "Bob", "Charlie"))
df2 <- data.frame(ID = 2:4, Salary = c(50000, 60000, 70000))
# Merge the DataFrames based on the "ID" column
merged_df <- merge(df1, df2, by = "ID", all = TRUE)
# Display the merged DataFrame
print(merged_df)
## ID Name Salary
## 1 1 Alice NA
## 2 2 Bob 50000
## 3 3 Charlie 60000
## 4 4 <NA> 70000
#n. Create two DataFrames
df1 <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30))
df2 <- data.frame(Name = c("Charlie", "David"), Age = c(35, 40))
# Append df2 to the end of df1
appended_df <- rbind(df1, df2)
# Display the appended DataFrame
print(appended_df)
## Name Age
## 1 Alice 25
## 2 Bob 30
## 3 Charlie 35
## 4 David 40
#o. Load the dplyr package
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Create a sample DataFrame
df <- data.frame(Group = c("A", "A", "B", "B", "C"),
Value = c(10, 15, 25, 20, 30))
# Select rows with maximum value in each group
result <- df %>%
group_by(Group) %>%
filter(Value == max(Value))
# Display the result
print(result)
## # A tibble: 3 × 2
## # Groups: Group [3]
## Group Value
## <chr> <dbl>
## 1 A 15
## 2 B 25
## 3 C 30
#p. Create two dataframes
df1 <- data.frame(ID = 1:4, Name = c("Alice", "Bob", "Charlie", "David"))
df2 <- data.frame(ID = 2:5, Salary = c(50000, 60000, 70000, 55000))
# Merge the dataframes based on the "ID" column
merged_df <- merge(df1, df2, by = "ID", all = TRUE)
# Display the merged dataframe
print(merged_df)
## ID Name Salary
## 1 1 Alice NA
## 2 2 Bob 50000
## 3 3 Charlie 60000
## 4 4 David 70000
## 5 5 <NA> 55000
#q.a. Read data from the console
data <- as.numeric(readline("Enter a number: "))
## Enter a number:
print(data)
## [1] NA
#q.b. reading data from csv file
data=read.csv("C:/Users/abhishek/Documents/Book1.csv")
data
## Org_Indiv First_Plus First_Name Last_Name
## 1 3-D Medical Services Llc Steven Bruce Steven Deitelzweig
## 2 Aa Doctors, Inc. Aakash Mohan Aakash Ahuja
## 3 Abbo, Lilian Margarita Lilian Margarita Lilian Abbo
## 4 Abbo, Lilian Margarita Lilian Margarita Lilian Abbo
## 5 Abbo, Lilian Margarita Lilian Margarita Lilian Abbo
## 6 Abdullah Raffee Md Pc Abdullah Abdullah Raffee
## 7 Abebe, Sheila Y Sheila Y Sheila Abebe
## 8 Abebe, Sheila Y Sheila Y Sheila Abebe
## 9 Abilene Family Foot Center Galen Chris Galen Albritton
## 10 Abolnik, Igor Z Igor Z Igor Abolnik
## 11 Abolnik, Igor Z Igor Z Igor Abolnik
## City State Category Cash Other Total
## 1 New Orleans LA Professional Advising 2625 0 2625
## 2 Paso Robles CA Expert-Led Forums 1000 0 1000
## 3 Miami FL Business Related Travel 0 448 448
## 4 Miami FL Meals 0 119 119
## 5 Miami FL Professional Advising 1800 0 1800
## 6 Flint MI Expert-Led Forums 750 0 750
## 7 Indianapolis IN Educational Items 0 47 47
## 8 Indianapolis IN Expert-Led Forums 825 0 825
## 9 Abilene TX Professional Advising 3000 0 3000
## 10 Provo UT Business Related Travel 0 396 396
## 11 Provo Ut Expert-Led Forums 1750 0 1750