0% found this document useful (0 votes)
2 views3 pages

R Simple Programs Dhs

The document provides a comprehensive overview of basic R programming concepts, including arithmetic, comparison, and logical operators. It also covers control structures like if-else statements and loops, as well as functions with default arguments and complex object returns. Additionally, it includes instructions for creating and customizing plots in R.

Uploaded by

nagaraja k v
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

R Simple Programs Dhs

The document provides a comprehensive overview of basic R programming concepts, including arithmetic, comparison, and logical operators. It also covers control structures like if-else statements and loops, as well as functions with default arguments and complex object returns. Additionally, it includes instructions for creating and customizing plots in R.

Uploaded by

nagaraja k v
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Simple R Programs

# Arithmetic Operators
a <- 10
b <- 3
addition <- a + b
print(addition)
subtraction <- a - b
print(subtraction)
multiplication <- a * b
print(multiplication )
division <- a / b
print(division )
modulus <- a %% b
print(modulus)
exponentiation <- a ^ b
print(exponentiation)

# Comparison Operators
greater_than <- a > b
less_than <- a < b
equal_to <- a == b
not_equal_to <- a != b

print(greater_than)
print(less_than)
print(equal_to)
print(not_equal_to)

# Logical Operators
logical_and <- TRUE && FALSE
logical_or <- TRUE || FALSE
logical_not <- !TRUE
print(logical_and)
print(logical_or)
print(logical_not)

# Control Structures
#if else
if (a > b)
{
result <- "a is greater than b"
}
else
{
result <- "a is not greater than b"
}
print(result)
#while loop
i <- 1
while (i < 6)
{
print(i)
i <- i + 1
}

#for loop
for (x in 1:10)
{
print(x)
}

# Function with Default Argument Values


x <- function(radius = 1, pi = 3.14159265359)
{
area <- pi * radius^2
return(area)
}
area_default <- x()
area_custom <- x(radius = 5, pi = 3.14)
print(area_default)
print(area_custom)

# Function Returning Complex Objects


create<- function(name, age, is_student) {
person <- list(Name = name,Age = age,IsStudent = is_student)
return(person)
}
person1 <- create("Alice", 25, TRUE)
person2 <- create("Bob", 30, FALSE)
print(person1)
print(person2)

# Draw one point in the diagram, at position 1 and 3


plot(1, 3)
# We need this line of code to show graphs in our compiler
plot(c(1, 8), c(3, 10))

#Multiple Points
plot(c(1, 2, 3, 4, 5), c(3, 7, 8, 9, 12))
x <- c(1, 2, 3, 4, 5)
y <- c(3, 7, 8, 9, 12)
plot(x, y)
#Sequences of Points
#If you want to draw dots in a sequence, on both the x-axis and the y-axis, use the :
operator:
plot(1:10)
#to draw a line
plot(1:10, type="l")
#Plot Labels
#The plot() function also accept other parameters, such as main, xlab and ylab if you want to
customize the graph with a main title and different labels for the x and y-axis:
plot(1:10, type="l",main="My Graph", xlab="The x-axis", ylab="The y axis")
#Graph Appearance
#There are many other parameters you can use to change the appearance of the points.
#Colors
#Use col="color" to add a color to the points:
plot(1:10, col="red", type="l")
#size
plot(1:10, cex=2)
#Point Shape
#Use pch with a value from 0 to 25 to change the point shape format:
plot(1:10, pch=7, cex=2)
plot(1:10, pch=8, cex=1)
plot(1:10, pch=10, cex=2)
#Line Graphs
plot(1:10, type="l")
Line Color

You might also like