Basic Concepts in R Programming
Basic Concepts in R Programming
variable.
There are two functions which are used to print the value of the variable i.e.,
print() and cat().
paste() to concatenate one or more variables
"\n" new line character
Output:
source("D:/R PROGRAMMING/operators.R", echo=TRUE)
> # Program to show arithmetic Operations in R with integers
> #Arithmetic operators + - * / %% %/% ^
> A <- 20 #value 20 ass .... [TRUNCATED]
> B = 10 #value 10 assigned using EQUAL TO operator "=" to variable B
> print(A+B) #Addition
[1] 30
> print(A-B) #subtraction
[1] 10
> print(A*B) #Multiplication
[1] 200
> print(A/B) #division
[1] 2
> print(A%%B) #Remainder
[1] 0
> print(A%/%B) #Quotient
[1] 2
> print(A^B) #power
[1] 1.024e+13
2. Program to declare and initialize Variables in R
# Program to declare and initialize variables in R
#using = operator using “=” equal to or assignment operator
print(paste(A, B))
cat(paste(A, B))
A=B=C="HAI"
print(A)
cat(paste(A, B))
cat(A)
cat(B)
cat(C)
cat("\n")
#"\n" new line character
cat(A,"\n")
cat(B,"\n")
cat(C,"\n")
Output:
> source("D:/R PROGRAMMING/var.R", echo=TRUE)
> # Program to declare and initialize in R
> #using = operator using “=” equal to or assignment operator
> R = "Welcome to Learn R"
> print(R)
[1] "Welcome to Learn R"
> # using <- operator leftward operator (“ <- ”)
> R <- "R programming "
> print(R)
[1] "R programming "
> #using -> operator rightward operator “->”
> "Variables in R" -> R
> print(R)
[1] "Variables in R"
> #paste() to concatenate one or more variables
> A <- "Apple"
> B <- "is Red Colour"
> print(paste(A, B))
[1] "Apple is Red Colour"
> cat(paste(A, B))
Apple is Red Colour
> A=B=C="HAI"
> print(A)
[1] "HAI"
> cat(paste(A, B))
HAI HAI
> cat(A)
HAI
> cat(B)
HAI
> cat(C)
HAI
> cat("\n")
> #"\n" new line character
> cat(A,"\n")
HAI
> cat(B,"\n")
HAI
> cat(C,"\n")
HAI
3. To get the Input from a user; compute Total and Average of Four Numbers
>{
>
4. R Booleans (Comparison and Logical Operators)
a <- 10
b <- 9
a>b
if (b > a) {
print ("b is greater than a")
} else {
print("b is not greater than a")
}
Output:
> source("D:/R PROGRAMMING/boolean.R", echo=TRUE)
>
> print(x)
[1] TRUE
> print(class(x))
[1] "logical"
> y <- F
> print(y)
[1] FALSE
> print(class(y))
[1] "logical"
[1] TRUE
[1] FALSE
> FALSE & TRUE
[1] FALSE
[1] FALSE
>
> x <- 10
> y <- 23
> z <- 12
[1] TRUE
> 10 > 9
[1] TRUE
> 10 == 9
[1] FALSE
> 10 < 9
[1] FALSE
> a <- 10
> b <- 9
>a>b
[1] TRUE
> if (b > a) {
+ } else {
+}
print(max(29,3,5,-80))
print(min(34.54,5,4,-3,3))
print(round(7.8999, digits=1))
a <- "885854351"
d = "Hai!
how are
you ?"
print(substr(a,3,6))
print(strsplit(a, ""))
print(tolower(c))
print(toupper(c))
print(cat(d))
>
> b <- 9
>
>
>
> e <- 4
>
>
> print(abs(a))
[1] 6.899
>
> print(sqrt(b))
[1] 3
>
> print(max(a,b,c))
[1] 9
>
> print(min(a,b,c))
[1] -6.899
>
>
> print(max(29,3,5,-80))
[1] 29
>
> print(min(34.54,5,4,-3,3))
[1] -3
>
> print(ceiling(c))
[1] 7
>
> print(floor(c))
[1] 6
>
> print(trunc(d))
[1] 1
>
>
> print(round(d,digits=3))
[1] 1.533
>
[1] 7.9
>
> print(log(e))
[1] 1.386294
>
>
>
> d = "Hai!
+ how are
+ you ?"
>
>
>
>
> print(substr(a,3,6))
[1] "5854"
>
[[1]]
[1] "8" "8" "5" "8" "5" "4" "3" "5" "1"
>
> print(tolower(c))
>
> print(toupper(c))
>
> print(cat(d))
Hai!
how are
you ?NULL