What is Data interfacing in R ?


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 :


    • 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 :


  • 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
#set the current directory
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”)
cat(“\nThe Employees in Marketing Category are \n”)
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”))
cat(“\nNew row was written into file\n”)
cat(“\nA new file was created using details from existing file\n”)
nd1=data.frame(Bal_Amnt = c(0,0,2000,0,1500,1000,0,1500,1000,3000,1500,5000))
cat(“A new column named Bal amnt was added\n”)

Leave Comment

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

clear formSubmit