0% found this document useful (0 votes)
15 views

R Lab-1

Uploaded by

r.j.buvna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

R Lab-1

Uploaded by

r.j.buvna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Big Data Analytics Using R Lab Record work Sem-VI

1. Create a vector in R and perform operations on it.


Aim:To Create a vector in R and perform operations on it.
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.
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:

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)

# Print the vector


print(my_vector)

# Access elements in the vector


print(my_vector[1]) # Access the first element (indexing in R starts from
1)

# Vector arithmetic operations


vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)

# 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)

# Sum of vector elements


sum_of_vector<- sum(my_vector)
print(sum_of_vector)

# Mean of vector elements


mean_of_vector<- mean(my_vector)
print(mean_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))

# Complex data type


my_complex<- 3 + 4i
print(my_complex)
print(class(my_complex))

5|P a ge
Big Data Analytics Using R Lab Record work Sem-VI

# Logical data type


my_logical<- TRUE
print(my_logical)
print(class(my_logical))

# Character data type


my_character<- "Hello, World!"
print(my_character)
print(class(my_character))

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.

3. Write code in R to demonstrate sum(), min(), max() and seq() functions.


Aim: To Write code in R to demonstrate sum(), min(), max() and seq() functions.
Procedure:
To execute R code in Windows, follow these steps:
1. Install R
2. Launch R Console or RStudio
3. Write R Code
- In the R Console or RStudio, you will see the R prompt (`>`) where you can type R
commands.

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)

# Calculate the sum of all elements in the vector


total_sum<- sum(my_vector)
print("Sum of the vector:")
print(total_sum)

# Find the minimum value in the vector


min_value<- min(my_vector)
print("Minimum value in the vector:")
print(min_value)

# Find the maximum value in the vector


max_value<- max(my_vector)
print("Maximum value in the vector:")
print(max_value)

# Generate a sequence of numbers from 1 to 10 with a step of 2


my_sequence<- seq(from = 1, to = 10, by = 2)
print("Generated sequence:")
print(my_sequence)

Output:
[1] "Sum of the vector:"
[1] 59

7|P a ge
Big Data Analytics Using R Lab Record work Sem-VI

[1] "Minimum value in the vector:"


[1] 2
[1] "Maximum value in the vector:"
[1] 20
[1] "Generated sequence:"
[1] 1 3 5 7 9

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."

# Using grep() to find positions of specific words


positions <- grep("Sample|Text|R", my_text, ignore.case = TRUE)
print("Positions of 'Sample', 'Text', and 'R' in the text:")

8|P a ge
Big Data Analytics Using R Lab Record work Sem-VI

print(positions)

# Using toupper() to convert text to uppercase


uppercase_text<- toupper(my_text)
print("Text in UPPERCASE:")
print(uppercase_text)

# Using tolower() to convert text to lowercase


lowercase_text<- tolower(my_text)
print("Text in lowercase:")
print(lowercase_text)

# Using substr() to extract a substring


substring_example<- substr(my_text, start = 8, stop = 21)
print("Extracted substring:")
print(substring_example)

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.

5. Create data frame in R and perform operations on it.


Aim: To Create data frame in R and perform operations on it.

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)
)

# Print the data frame


print("Original Data Frame:")
print(my_data)

# Accessing specific columns


print("Names in the data frame:")
print(my_data$Name)

# Filtering rows based on a condition


young_students<- my_data[my_data$Age< 25, ]
print("Students younger than 25:")
print(young_students)

# Adding a new column


my_data$Grade<- ifelse(my_data$Score>= 90, "A", "B")
print("Data Frame with Grade column:")

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

# Reading data from another directory


x<-read.table("D://Data//myfile.txt", header = FALSE)

# 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")

# display csv file


print(read_data)

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

10 OCT 359 407 461


11 NOV 310 362 390
12 DEC 337 405 432

Result:
The Program has been Written and executed successfully.

7. Write code in R to find out whether number is prime or not.


Aim: To Write code in R to find out whether number is prime or not.
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:
# 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

# Test the function with some numbers


test_numbers<- c(2, 7, 10, 13, 20, 29)

for (num in test_numbers) {


if (is_prime(num)) {
print(paste(num, "is a prime number."))
} else {
print(paste(num, "is not a prime number."))
}
}

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

7. Exit R Console or RStudio


-

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.

9. Write a Program to demonstrate PieChart in R


Aim: To Write a Program to demonstrate PieChart in R

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)

result <- pie(expenditure,


main = "Monthly Expendit
Expenditure Breakdown"
)

print(result)
Output:

Result:
The Program has been Written and executed successfully.

10. Write a program to demonstrate histogram in R.

17 | P a g e
Big Data Analytics Using R Lab Record work Sem-VI

Aim:To Write a program to demonstrate histogram in R.


Procedure:
To execute R code in Windows, follow these steps:
1. Install
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:
temperatures <- c(67 ,72 ,74 ,62 ,76 ,66 ,65 ,59 ,61 ,69 )

# histogram of temperatures vector


result <- hist(temperatures,
main = "Histogram of Temperature",
xlab = "Temperature in degrees Fahrenheit"
)

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy