Monday, August 21, 2023

R Language Fetching Data From Excel Sheet

R Language Query Data on Excel Sheet Column Header :-


Install xlsx Package :- 
install.packages("xlsx")

Verify and Load the "xlsx" Package:-
# Verify the package is installed.
any(grepl("xlsx",installed.packages()))

# Load the library into R workspace.
library("xlsx")

Reading the Excel File:-
# Read the first worksheet in the file input.xlsx.
data <- read.xlsx("student_list.xlsx", sheetIndex = 1)
print(data)
#Data Query in Excel Sheet Column Header :-
retval <- subset(data, Blood.Group == "O+")
print(retval)

Reading a CSV File :-
data <- read.csv("Mutual Fund List.csv")  
print(data)
print(is.data.frame(data))
print(ncol(data))
print(nrow(data))

# Get the max salary from data frame.
sal <- max(data$salary)
print(sal)

# Get the max salary from data frame.
sal <- max(data$salary)

# Get the person detail having max salary.
retval <- subset(data, salary == max(salary))
print(retval)

#Get all the people working in IT department
retval <- subset( data, dept == "IT")
print(retval)

#Get the persons in IT department whose salary is greater than 600
info <- subset(data, salary > 600 & dept == "IT")
print(info)

#Get the people who joined on or after 2014
retval <- subset(data, as.Date(start_date) > as.Date("2014-01-01"))
print(retval)

# Write filtered data into a new file.
write.csv(retval,"output.csv")
newdata <- read.csv("output.csv")
print(newdata)

No comments:

Post a Comment