What is Data interfacing in R ?

Description

To know how to interface different data files in R.

    • Files stored in locations outside the R environment can be accessed by R,
    • The Read and write operation of a file can be done through R.

Changing current directory :

    • getwd() – To get the current working directory
    • setwd(“directory path”) – To set the working directory to user desirable directory

Reading a csv file :

    • The files cab be read using the function read.csv()

Syntax :

Variable_name=read.csv(“filename.csv”)

    • To read a particular file the working directory must be set to the destined file location.
    • There are few functions to know details about the file that is read.They are
    • is.dataframe(variable) – To check whether the file output is in data frame or not
    • ncol(variable) – No.of columns in the file
    • nrow(variable) – No.of rows in the file

To access the columns :

    • Variable_name$columnname

Ex: data$Name,data$ID,data$Salary

Analyzing the file data :

    • To get details meeting particular criteria
    • subset(data,Salary==max(data$Salary) – To get the employee details who gets maximum salary
    • subset(data,Dep==”Marketing”) – To get employee details belonging to Mraketing department

Adding a new row to the existing file :

    • The function rbind() can be used to add a new row
    • Variable_name=rbind(Variable_name,new_row_data)
    • Note that the variable name is the name in which the file was read and stored in R

Adding a new column to the existing file :

    • The function cbind() can be used to add a new row
    • Variable_name=cbind(Variable_name,new_colm_data)
    • Note that the variable name is the name in which the file was read and stored in R

Writing csv file :

    • write.csv () is used to write datas to csv file

Syntax :

write.csv(newdata,”filename.csv”,rownames=FALSE)

  • newdata must be of type dataframe
  • rownames=False is default to ignore the row names
  • When copying one csv file data to an another csv file.

#get the current working directory
print(getwd())
#set the current directory
setwd(“/home/soft23/soft23/Aks”)
print(getwd())
data<-read.csv(“Emp.csv”)
print(data)
cat(“Is the file in dataframe format : “,is.data.frame(data),”\n”)
cat(“The number of rows in the file : “,nrow(data))
cat(“\nThe number of columns in the file : “,ncol(data))
cat(“\nThe maximum salary given is “,max(data$SALARY))
cat(“\nThe Employee getting maximum salary is\n”)
print(subset(data,SALARY==max(SALARY)))
cat(“\nThe Employees in Marketing Category are \n”)
print(subset(data,DEPT==”Marketing”))
nd=data.frame(ID=c(11,12),NAME=c(“Amutha”,”Saranya”),SALARY=c(14000,14000),TA=c(600,600),No.Of.DAYS=c(28,28),No.Of.Leaves=c(1,1),DEPT=c(“Data entry”,”Data entry”))
data=rbind(data,nd)
write.csv(data,”Emp.csv”,row.names=FALSE)
cat(“\nNew row was written into file\n”)
print(read.csv(“Emp.csv”))
newentry=subset(data,DEPT==”Marketing”)
write.csv(newentry,”Mdept.csv”,row.names=FALSE)
cat(“\nA new file was created using details from existing file\n”)
print(read.csv(“Mdept.csv”))
nd1=data.frame(Bal_Amnt = c(0,0,2000,0,1500,1000,0,1500,1000,3000,1500,5000))
data=cbind(data,nd1)
write.csv(data,”Emp.csv”,row.names=FALSE)
cat(“A new column named Bal amnt was added\n”)
print(read.csv(“Emp.csv”))

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit