R Lab-1
R Lab-1
1|P a ge
Big Data Analytics Using R Lab Record work Sem-VI
- Use the "source()" function in R Console or RStudio to run the entire script:
`source("path/to/your/script.R")`.
- In RStudio, open the R script and click the "Source" button.
7. Exit R Console or RStudio:
- To exit the R Console or RStudio, type `q()` and press Enter.
Program:
# Create a vector
my_vector<- c(1, 2, 3, 4, 5)
# Addition
result_add<- vector1 + vector2
print(result_add)
# Subtraction
result_sub<- vector2 - vector1
print(result_sub)
# Multiplication
result_mul<- vector1 * vector2
print(result_mul)
2|P a ge
Big Data Analytics Using R Lab Record work Sem-VI
# Division
result_div<- vector2 / vector1
print(result_div)
# Element-wise operations
vector3 <- c(2, 4, 6)
vector4 <- c(3, 6, 9)
# Element-wise maximum
result_max<- pmax(vector3, vector4)
print(result_max)
# Element-wise minimum
result_min<- pmin(vector3, vector4)
print(result_min)
# Vector length
length_of_vector<- length(my_vector)
print(length_of_vector)
# Sorting a vector
sorted_vector<- sort(my_vector)
print(sorted_vector)
3|P a ge
Big Data Analytics Using R Lab Record work Sem-VI
Output:
[1] 1 2 3 4 5
[1] 1
[1] 5 7 9
[1] 3 3 3
[1] 4 10 18
[1] 4.0 2.5 2.0
[1] 3 6 9
[1] 2 4 6
[1] 5
[1] 15
[1] 3
[1] 1 2 3 4 5
Result:
The Program has been Written and executed successfully.
2. Create integer, complex, logical, character data type objects in R and print their
values and their class using print and class functions.
Aim:To Create integer, complex, logical, character data type objects in R and print their
values and their class using print and class functions.
Procedure:
To execute R code in Windows, follow these steps:
1. Install R:
- Download the latest version of R for Windows from the official R website (https://cran.r-
project.org/bin/windows/base/).
- Run the installer and follow the installation instructions.
2. Launch R Console or RStudio:
- After installation, you can either use the R Console (a command-line interface) or RStudio
(an integrated development environment for R).
Using R Console:
- Click on the Windows Start button and search for "R" to find the R Console.
- Click on "R x.y.z" (where x.y.z represents the version number) to open the R Console.
Using RStudio:
- If you have installed RStudio, launch it from the Start menu or desktop shortcut.
4|P a ge
Big Data Analytics Using R Lab Record work Sem-VI
3. Write R Code:
- In the R Console or RStudio, you will see the R prompt (`>`) where you can type R
commands.
4. Execute R Code:
- To execute a single line of code, type the code and press Enter. The result, if any, will be
displayed immediately below the code.
- To execute multiple lines of code, select the lines you want to execute and press Ctrl +
Enter or click the "Run" button in RStudio.
5. Save R Script (Optional):
- If you have a sequence of R commands that you want to reuse or save for future use, you
can create an R script.
- Click on "File" in RStudio and then select "New File" > "R Script" to create a new R
script.
- Write or paste your R code into the script editor.
- Save the R script with a .R extension, such as "my_script.R".
6. Run R Script (Optional):
- To run an R script, you can either:
- Use the "source()" function in R Console or RStudio to run the entire script:
`source("path/to/your/script.R")`.
- In RStudio, open the R script and click the "Source" button.
7. Exit R Console or RStudio:
- To exit the R Console or RStudio, type `q()` and press Enter.
Program:
# Integer data type
my_integer<- 42
print(my_integer)
print(class(my_integer))
5|P a ge
Big Data Analytics Using R Lab Record work Sem-VI
Output:
[1] 42
[1] "integer"
[1] 3+4i
[1] "complex"
[1] TRUE
[1] "logical"
[1] "Hello, World!"
[1] "character"
Result:
The Program has been Written and executed successfully.
6|P a ge
Big Data Analytics Using R Lab Record work Sem-VI
4. Execute R Code
5. Save R Script (Optional)
6. Run R Script (Optional)
7. Exit R Console or RStudio:
- To exit the R Console or RStudio, type `q()` and press Enter.
Program:
# Create a vector of numbers
my_vector<- c(10, 5, 7, 2, 15, 20)
Output:
[1] "Sum of the vector:"
[1] 59
7|P a ge
Big Data Analytics Using R Lab Record work Sem-VI
Result:
The Program has been Written and executed successfully.
4. Write code in R to manipulate text in R using grep(), toupper(), tolower() and substr()
functions.
Aim:To Write code in R to manipulate text in R using grep(), toupper(), tolower() and
substr() functions.
Procedure:
To execute R code in Windows, follow these steps:
1. Install R
2. Launch R Console or RStudio
3. Write R Code
4. Execute R Code
5. Save R Script (Optional)
6. Run R Script (Optional)
7. Exit R Console or RStudio
- To exit the R Console or RStudio, type `q()` and press Enter.
Program:
# Sample text
my_text<- "Hello, World! This is a Sample Text for Text Manipulation
in R."
8|P a ge
Big Data Analytics Using R Lab Record work Sem-VI
print(positions)
Output:
[1] "Positions of 'Sample', 'Text', and 'R' in the text:"
[1] 24 29 58
[1] "Text in UPPERCASE:"
[1] "HELLO, WORLD! THIS IS A SAMPLE TEXT FOR TEXT MANIPULATION IN R."
[1] "Text in lowercase:"
[1] "hello, world! this is a sample text for text manipulation in r."
[1] "Extracted substring:"
[1] "World! This is"
Result:
The Program has been Written and executed successfully.
9|P a ge
Big Data Analytics Using R Lab Record work Sem-VI
Procedure:
To execute R code in Windows, follow these steps:
1. Install R
2. Launch R Console or RStudio
3. Write R Code
4. Execute R Code
5. Save R Script (Optional)
6. Run R Script (Optional)
Program:
# Create a data frame
my_data<- data.frame(
Name = c("Alice", "Bob", "Charlie", "David", "Emma"),
Age = c(25, 30, 22, 28, 24),
Score = c(95, 88, 78, 92, 85)
)
10 | P a g e
Big Data Analytics Using R Lab Record work Sem-VI
print(my_data)
# Aggregating data
average_age<- mean(my_data$Age)
print("Average age of students:")
print(average_age)
max_score<- max(my_data$Score)
print("Maximum score:")
print(max_score)
Output:
[1] "Original Data Frame:"
Name Age Score
1 Alice 25 95
2 Bob 30 88
3 Charlie 22 78
4 David 28 92
5 Emma 24 85
[1] "Names in the data frame:"
[1] "Alice" "Bob" "Charlie" "David" "Emma"
[1] "Students younger than 25:"
Name Age Score
3 Charlie 22 78
5 Emma 24 85
[1] "Data Frame with Grade column:"
Name Age Score Grade
1 Alice 25 95 A
2 Bob 30 88 B
3 Charlie 22 78 B
4 David 28 92 A
5 Emma 24 85 B
[1] "Average age of students:"
11 | P a g e
Big Data Analytics Using R Lab Record work Sem-VI
[1] 25.8
[1] "Maximum score:"
[1] 95
Result:
The Program has been Written and executed successfully.
6. Import data into R from text and excel files using read.table () and read.csv ()
functions.
Aim:To Import data into R from text and excel files using read.table () and read.csv ()
functions.
Procedure:
To execute R code in Windows, follow these steps:
1. Install R
2. Launch R Console or RStudio
3. Write R Code
4. Execute R Code
5. Save R Script (Optional)
6. Run R Script (Optional)
7. Exit R Console or RStudio
Program:
read.table ():
# R program to read a text file
# print x
print(x)
Output:
V1 V2 V3
1 100 a1 b1
2 200 a2 b2
12 | P a g e
Big Data Analytics Using R Lab Record work Sem-VI
3 300 a3 b3
read.csv ():
To demonstrate how we read CSV files in R, let's suppose we have a CSV file
named airtravel.csv with following data:
Month,1958,1959,1960
JAN,340,360,417
FEB,318,342,391
MAR,362,406,419
APR,348,396,461
MAY,363,420,472
JUN,435,472,535
JUL,491,548,622
AUG,505,559,606
SEP,404,463,508
OCT,359,407,461
NOV,310,362,390
DEC,337,405,432
Program:
# read airtravel.csv file from our current directory
read_data<- read.csv("airtravel.csv")
Output:
Month, 1958, 1959, 1960
1 JAN 340 360 417
2 FEB 318 342 391
3 MAR 362 406 419
4 APR 348 396 461
5 MAY 363 420 472
6 JUN 435 472 535
7 JUL 491 548 622
8 AUG 505 559 606
9 SEP 404 463 508
13 | P a g e
Big Data Analytics Using R Lab Record work Sem-VI
Result:
The Program has been Written and executed successfully.
Program:
# Function to check if a number is prime
is_prime<- function(num) {
if (num<= 1) {
return(FALSE) # Numbers less than or equal to 1 are not prime
}
for (i in 2:sqrt(num)) {
if (num %% i == 0) {
return(FALSE) # If any divisor is found, the number is not prime
}
}
return(TRUE) # If no divisors are found, the number is prime
}
14 | P a g e
Big Data Analytics Using R Lab Record work Sem-VI
Output:
[1] "2 is a prime number."
[1] "7 is a prime number."
[1] "10 is not a prime number."
[1] "13 is a prime number."
[1] "20 is not a prime number."
[1] "29 is a prime number."
Result:
The Program has been Written and executed successfully.
8. Print numbers from 1 to 100 using while loop and for loop in R.
Aim: To Print numbers from 1 to 100 using while loop and for loop in R.
Procedure:
To execute R code in Windows, follow these steps:
1. Install R
2. Launch R Console or RStudio
3. Write R Code
4.Excutes R code
5. Save R Script (Optional)
6. Run R Script (Optional)
15 | P a g e
Big Data Analytics Using R Lab Record work Sem-VI
Program:
# Print numbers from 1 to 100 using a while loop
num<- 1
while (num<= 100) {
print(num)
num<- num + 1
}
For:
# Print numbers from 1 to 100 using a for loop
for (num in 1:100) {
print(num)
}
Output:
[1] 1
[1] 2
[1] 3
...
[1] 98
[1] 99
[1] 100
Result:
The Program has been Written and executed successfully.
Procedure:
To execute R code in Windows, follow these steps:
16 | P a g e
Big Data Analytics Using R Lab Record work Sem-VI
1. Install R.
2. Launch R Console or RStudio
3. Write R Code
4. Execute R Code
5. Save R Script (Optional
6. Run R Script (Optional)
7. Exit R Console or RStudio
Program:
expenditure <- c(600, 300, 150, 100, 200)
print(result)
Output:
Result:
The Program has been Written and executed successfully.
17 | P a g e
Big Data Analytics Using R Lab Record work Sem-VI
Program:
temperatures <- c(67 ,72 ,74 ,62 ,76 ,66 ,65 ,59 ,61 ,69 )
print(result)
18 | P a g e
Big Data Analytics Using R Lab Record work Sem-VI
Output:
Result:
The Program has been Written and executed successfully.
19 | P a g e