READING AN EXCEL FILE
To read an excel file read_excel() function is used. This function is present in the package named
“readxl”. Hence before using this function the package "readxl" has to be installed.
A package is a collection of functions, data, and other code that are highly organized in a
reusable manner. R packages are an essential part of the R ecosystem and contribute
greatly to the flexibility and power of the language.
The base packages which are come with the default installation of R. But some other packages
which is not comes under the base packages can be installed using the [Link]() function
in R.
To install the same the following syntax (Syntax in R refers to the set of rules and conventions
that must be followed when writing R code.) can be used.
[Link]("package_name")
Replace "package_name" with the name of the package you want to install.
Eg. [Link]("readxl")
[Link]("ggplot2")
Once installed, a package, it can be loaded into an R session using the library() function,
which makes all the functions and data in the package available to the user. The library() function
is used to load packages into the current R session. The syntax for the library() function is as
follows:
library(package name)
Replace "package_name" with the name of the package
Eg. library(readxl)
Before reading a file it is necessary to set the working directly properly, for the same two common
functions getwd() and setwd () are used.
getwd() : the getwd() function is used to get the current working directory :
setwd () : the setwd() function is to set the working directory to a specific directory
eg. setwd("C:\\Users\\vimalkumar\\Desktop")
setwd("C:\\Users\\KSBB\\Desktop")
or setwd("C:/Users/KSBB/Desktop ")
setwd("C:/Users/vimalkumar/Desktop")
to read an excel file read_excel() function is used
Eg.
aa <- read_excel("[Link]")
bb <- read_excel("[Link]")
ac <- read_excel("[Link]")
To select particular sheet we have to specify the sheet number
data <-read_excel("[Link]", sheet=2)
to select a particular field “$” is used
abc <- ac$'Field 1’
mean (data$'Field 1')
to delete a particular field the following method can be adopted
eg. xy<- read_excel("[Link]")
View(xy)
xy$Dose <- NULL
View(xy)
xy$Treatment <- NULL
View(xy)
To select specific number of rows and column tablename[row, column] is used
abc <- ac[, 2:3]
print(abc)
ab1 <- ac[5, 2:3]
print(ab1)
ab2 <- ac[2:5, 2:3]
print(ab2)
to read an excel file from its location read_excel(path) is used
Syntax : read_excel (path\\name of the [Link]-extension)
eg. dd <- read_excel("C:\\Users\\vimalkumar\\Desktop\\ rawdata\\[Link]")
to read a csv file , function [Link]("[Link]") is used
eg. prg <- [Link]("[Link]") is used
co2 <- [Link]("[Link]")
print(co2)
to read a text file function [Link](file, header=TRUE, sep="\t") is used
file – A file location.
header – Whether the first line describes the column names.
sep – The table delimiter, often times a tab (\t) or comma.
Eg.
DNA <- [Link]("[Link]", header = FALSE,sep = ",")
[Link]() function also allows to read data from a text file or a table.
data <- [Link](file = "my_file.txt", header = TRUE)
data1 <- [Link](file = "[Link]", header = TRUE,sep = ",")
Reading .bed files
[Link]
from where you can download “.bed” files
to read a .bed file the same function of [Link]() is used
life1 <-[Link]("C:\\Users\\vimalkumar\\Downloads\\[Link]",
header=TRUE, sep="\t")
we can also import data from websites… try with the following site for reading a text file
form awebsite
pbc <- [Link] ('[Link]
print (pbc)
estrol <- [Link] ('[Link]
estrol
***************************************************
WRITE DATA INTO A CSV FILE
To write a data into a csv file, syntax : [Link](data source,"[Link]")
Eg.
data1 <- [Link]("[Link]")
print(data1)
data2 <- data1[1:10, 1:5]
print(data2)
[Link](data2,"[Link]")
[Link](data2,"[Link]",[Link] = FALSE)
C()
The c function in R programming stands for 'combine.' It combines multiple values into a vector
or list.
c(value1, value2,value3,…)
height <- c(21,25,26,30)
print(height)
weight <- c(45,50,65,59)
print(weight)
combine <- c(height, weight)
print(combine)
d <- [Link](height, weight)
list(d)
ht <- c(25,50,55,70,80)
letr <- c('A', 'C', 'L', 'M', 'O')
ag <- c(10,8,9,11,9.5)
ggg <- [Link](ht, letr,ag)
list(ggg)
[Link](ggg,"[Link]",[Link] = FALSE)
[Link](ggg, file="[Link]")
[Link](ggg, file="[Link]",[Link] = FALSE)
to select a csv file directly :
eg. ab <- [Link]([Link]())
to select a text file directly
eg. lif <- [Link]([Link]())