0% found this document useful (0 votes)
13 views4 pages

R Queries

The document contains R code for data manipulation using the sqldf package to query CSV files. It demonstrates various SQL operations such as filtering employees by salary, department, and sorting data, as well as aggregating functions like sum and count. Additionally, it includes commands to read data from two CSV files and perform analyses on student admissions data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

R Queries

The document contains R code for data manipulation using the sqldf package to query CSV files. It demonstrates various SQL operations such as filtering employees by salary, department, and sorting data, as well as aggregating functions like sum and count. Additionally, it includes commands to read data from two CSV files and perform analyses on student admissions data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

read.csv("/Users/arya/Desktop/Documents/Study/Term2/DA/S11/input.

csv")
a=read.csv("/Users/arya/Desktop/Documents/Study/Term2/DA/S11/input.csv")

View(a) #case sensitive


sqldf("select * from a")

#Which employees have a salary less than 623?


sqldf("select * from a where salary<623")

#Who earns less than 623, and what are their names and departments?
sqldf("select naMe,dept from a where salary<623")
sqldf("select name,dept from a where salary<623")
#When using sqldf in R, column name case insensitivity occurs
#because the underlying SQL engine used by sqldf (like SQLite)
#is case-insensitive for column names.

#Which employees in the IT department have a salary less than 623?


sqldf("select * from a where dept='IT' and salary<623")
sqldf("select * from a where dept='IT' or dept='HR'")

#you can use more than one AND/OR operator in an sqldf query to combine multiple conditions.

#Which employees do not work in the HR department?


sqldf("select * from a where dept!='HR'")
#What is the total sum of salaries in the dataset?
sqldf("select sum(salary) from a")
sqldf("select sum(salary) as Total_Salary from a")

#*count()
#*min()
#*max()
#*distinct()
#*variance() or stddev()
#avg()

sqldf("select count(distinct Dept) from a")


sqldf("select distinct(dept) from a")

#retrieve all data from the data table and sort it in ascending order by the name column
sqldf("select * from a order by name")
sqldf("select * from a order by dept,name")
sqldf("select * from a order by name desc")
sqldf("select * from a order by dept desc,name asc")

#retrieve all data from the data table where the name starts with the letter 'R'
sqldf("select * from a where name like '%R'")
sqldf("select * from a where name like 'R%'")
sqldf("select * from a where name like '%A%'")
sqldf("select * from a where name like '__a%'")
b=read.csv("/Users/arya/Desktop/UCBAdmissions.csv")

#total admitted student


sqldf("select sum(Freq) from b where Admit='Admitted'")

#return total rejected female


sqldf("select sum(freq) from b where admit='Rejected' and Gender='Female'")

#department wise total admitted student


sqldf("select dept,sum(freq) from b where admit='Admitted' group by Dept")
sqldf("select admit,sum(freq) from b group by Admit")
install.packages("xlsx")
install.packages("sqldf")

You might also like