R Lab
R Lab
10. Plot the bar chart and pie chart on sample data
Arithmetic OPerator
x <- 5
y <- 16
x+y
[1] 21
x-y
[1] -11
x*y
[1] 80
y/x
[1] 3.2
y%/%x
[1] 3
y%%x
[1] 1
y^x
[1] 1048576
Relational Operator
x <- 5
y <- 16
x<y
[1] TRUE
x>y
[1] FALSE
x<=5
[1] TRUE
y>=20
[1] FALSE
y == 16
[1] TRUE
x != 5
[1] FALSE
Logical OPerator
x <- c(TRUE,FALSE,0,6)
y <- c(FALSE,TRUE,FALSE,TRUE)
!x
[1] FALSE TRUE TRUE FALSE
x&y
[1] FALSE FALSE FALSE TRUE
> x&&y
[1] FALSE
x|y
[1] TRUE TRUE FALSE TRUE
x||y
[1] TRUE
Assignment Operator
> x <- 5
>x
[1] 5
>x=9
>x
[1] 9
> 10 -> x
>x
[1] 10
2. Write an R Program to Check if a Number is Odd or Even
Output 1
Enter a number: 89
[1] "89 is Odd"
Output 2
Enter a number: 0
[1] "0 is Even"
3. Write an R Program to check if the given Number is a Prime Number.
num = as.integer(readline(prompt = "Enter a number: "))
# num = 15
isPrime = 0
if (num > 1) {
isPrime = 1
for (i in 2: (num - 1)) {
if ((num %% i) == 0) {
isPrime = 0
break
}
}
}
if (num == 2) isPrime = 1
if (isPrime == 1) {
print(paste(num, "is a prime number"))
} else {
print(paste(num, "is not a prime number"))
}
OUTPUT
Enter a number: 15
[1] "15 is not a prime number"
Enter a number: 13
[1] "13 is a prime number"
4. Write an R Program to Find the Factorial of a Number
Output:
Output
Output
How many terms? 9
[1] "Fibonacci sequence:"
[1] 0
[1] 1
[1] 1
[1] 2
[1] 3
[1] 5
[1] 8
[1] 13
[1] 21
7. Create the following data frame, afterwards invert Sex for all individuals.
NAME AGE HEIGHT WEIGHT sex
Lilly 25 177 57 F
Raja 32 187 77 M
Sundar 30 165 62 M
Ramya 44 160 58 F
Rakesh 39 170 69 M
levels(df$Sex)
OUTPUT
df
## Lilly 25 177 57 F
## Raja 31 163 69 M
## Sundar 52 179 75 M
## Ramya 76 163 70 F
## Rakesh 49 183 83 M
8. Write an R Program to create a Matrix from a Vector using dim() function, cbind() function and
rbind().
> mat1
# Loading package
library(dplyr)
# Adding size
ggplot(data = mtcars,
aes(x = hp, y = mpg, size = disp)) + geom_point()
# Histogram plot
ggplot(data = mtcars, aes(x = hp)) +
geom_histogram(binwidth = 5)
10. Write an R Program to plot the bar chart and pie chart on sample data
[[1]]
Name Age
1 Juan 22
2 Alcaraz 15
[[2]]
Name Age
1 Yiruma 46
2 Bach 89
3 Ludovico 72
list(dataframe1, dataframe2)