Module II Notes - 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

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 install.packages() 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.
install.packages("package_name")
Replace "package_name" with the name of the package you want to install.

Eg. install.packages("readxl")
install.packages("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("rabbit.xlsx")

bb <- read_excel("index.xlsx")

ac <- read_excel("name.xlsx")

To select particular sheet we have to specify the sheet number

data <-read_excel("program.xlsx", 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("rabbit.xlsx")

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 file.file-extension)

eg. dd <- read_excel("C:\\Users\\vimalkumar\\Desktop\\ rawdata\\plantdata.xlsx")

to read a csv file , function read.csv("filename.csv") is used

eg. prg <- read.csv("program.csv") is used

co2 <- read.csv("CO2uptake.csv")


print(co2)

to read a text file function read.delim(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 <- read.delim("dna.txt", header = FALSE,sep = ",")


read.table() function also allows to read data from a text file or a table.

data <- read.table(file = "my_file.txt", header = TRUE)

data1 <- read.table(file = "dna.txt", header = TRUE,sep = ",")

Reading .bed files

https://genome.ucsc.edu/ENCODE/

from where you can download “.bed” files


to read a .bed file the same function of read.delim() is used
life1 <-read.delim("C:\\Users\\vimalkumar\\Downloads\\wgEncodeRegDnaseClustered.bed",
header=TRUE, sep="\t")

we can also import data from websites… try with the following site for reading a text file
form awebsite

pbc <- read.csv ('http://courses.washington.edu/b517/Datasets/PBCshort.csv')

print (pbc)

estrol <- read.delim ('http://courses.washington.edu/b517/Datasets/estradiol.txt')

estrol

***************************************************

WRITE DATA INTO A CSV FILE


To write a data into a csv file, syntax : write.csv(data source,"name.csv")

Eg.

data1 <- read.csv("who.csv")


print(data1)

data2 <- data1[1:10, 1:5]


print(data2)

write.csv(data2,"output.csv")

write.csv(data2,"output1.csv",row.names = 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 <- data.frame(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 <- data.frame(ht, letr,ag)
list(ggg)
write.csv(ggg,"output3.csv",row.names = FALSE)

write.table(ggg, file="vkk.text")
write.table(ggg, file="vkk.text",row.names = FALSE)

to select a csv file directly :

eg. ab <- read.csv(file.choose())

to select a text file directly


eg. lif <- read.delim(file.choose())

You might also like