UNIT 2
UNIT 2
*CSV files-read.csv()
data<-read.csv(“data.csv”)
*Excel files-library(readxl)
data<-read(“data.xlsx”)
*Other data-read.delim()
data<-read.delim(“datasetfile.text”,header=FALSE)
data
file.choose(),read_tsv()-readr package,read_file()
Writing files in R
*CSV-write.csv()
data<-data.frame(Name=c(“Meera”,”Vani”),Age=c(25,25))
write.csv(data,”output.csv”,row.names=FALSE)
*Excel-xlsx package
write.xlsx(data,file=”result.xlsx”,sheetName=”mydata”,app
end=FALSE)
*Text-.txt
Programming with input and output statements
readline()-input in string and convert to desired data
type
Functions
*n=readline();
*as.integer(n);
*as.numeric(n);
*as.complex(n);
*as.character(n);
as.Date(n);
example
> n<-readline()
555
>n
[1] "555"
> n=as.integer(n)
>n
[1] 555
scan()
input method
list=scan()
list
Reading file using scan()
sf=scan(“fileString.txt”,what=” “)
df=scan(“fileDouble.txt”,what=double())
OUTPUT STATEMENTS IN R
print()
cat()
message()
sprintf()
write.table()
{
str="Hello Hi";
num=123;
dec=34.4;
str;
print(str);
sprintf(“%s is a string",str);
}
[1] "Hello Hi is a string"
sprintf("%d is a number",num);
[1] "123 is a number"
sprintf("%f is a dloat value",dec);
[1] "34.400000 is a dloat value"
> x<-42
> y<-"Hello"
> print(x)
[1] 42
> cat("the value of x is",x,"and y is",y,"\n")
the value of x is 42 and y is Hello
CONDITION STATEMENTS
*If statement
*if-else statement
*if -else if -else statement
*switch statement
if statement
executes the statement if condition is true
Syntax- if(condition)
{
-------code------
}
> x<-10
> if(x>5)
+{
+ cat("x is greater than 5
\n")
+}
x is greater than 5
if- else statement
else block gets executed if the condition is false
syntax- if(condition)
{
---code---
}
else
{
---code---
}
> x<-3
> if(x>5)
+{
+ cat("x is greater than 5\n")
+ }else{
+ cat("x is lesser than 5 \n")
+}
x is lesser than 5
if-else if-else statement
test multiple conditions
syntax-if(condition1){
code---
}else if(condition2){
code---
}else{
code
}
> x<-8
> if(x>10){
+ cat("x is greater than 10 \n")
+ }else if(x>5){
+ cat("x is greater than 5 but lesser than 10\n")
+ }else{
+ cat("x is not greater than 5 \n")
+}
x is greater than 5 but lesser than 10
switch statement
compares different values ,multiway bracnch statemen t
syntax-
switch(expression,
value1={
code---
},
value2={
code---
},
default={
code--
}
}
)
> day<-"sunday"
> switch(1,
+ "monday"=cat("2nd day of the week \n"),
+ "saturday"=cat("weeend \n"),
+ default=cat("regular day \n")
+ )
2nd day of the week
LOOPS
Repeats a block code multiple times ,iteration,condition is met.
*for loop
*while loop
*repeat loop
for loop
entry controlled loop ,condition is tested first and then body is executed
syntax-
for(var in vector)
{
statements;
}
(or)
for(var in seq){
code---
}
for loop 1 to 5 list in concatenation
> for(x in 1:5) > for(x in c(-
+{ 5,6,9,15,20))
+ print(x^2) +{
+} + print(x)
[1] 1 +}
[1] 4 [1] -5
[1] 9 [1] 6
[1] 16 [1] 9
[1] 25 [1] 15
[1] 20
nested for loop for loop for list
> x<-1
> while(x<6)
+{
+ print(x)
+ x=x+1
+}
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
repeat loop
excutes till break statement
function()
syntax
function_name<-function(arg1,arg2...)
{ code---
return(result)
}
calling a function
> add<-function(a,b){
+ result<-a+b
+ return(result)
+}
> result<-add(3,5)
> cat("the result",result,"\n")
the result 8
>
Built in functions
*sum() > print(sum(5:10))
*max() [1] 45
*min() > print(max(5:10))
*seq() [1] 10
*mean() > print(min(5:10))
[1] 5
> print(seq(5,10))
[1] 5 6 7 8 9 10
> print(mean(5:10))
[1] 7.5
user defined functions
function arguments
res<-add(a=5,b=3)
res1<-add(5,3)
function with default values
power<-function(base,exponent=2){
res<-base^exponent
return(res)
}
ex-2
res1<-power(2)
res2<-power(2,3)
function with single parameter
area_square<-
function(side){
+ area=side*side
+ return(area)
+}
> print(area_square(8))
64
multiple parameter
> si=function(p,r,t)
+{
+ res=(p*r*t)/100
+ return(res)
+}
> print(si(1000,10,2))
[1] 200
without parameter and without return
> fun=function()
+{
+ print("hello")
+}
> print(fun())
[1] "hello"
[1] "hello"
variable scoping
lexical scoping-variables are local by default
<<- assign global variables
check- installed.packages()
update-update.packages()
loading packages in R
library(dplyr)
require(dplyr)
Profvis package
Profiling R,visualizing code execution
> profvis({
+ for(i in 1:1000000){
+ sqrt(i)
+}
+}
Microbenchmark package
measuring execution time
Benchmarking with bench package
detailed information
Benchmarking with rbenchmark package
compares execution time of multiple functions