Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

R-Programming - Ai&ds 10 Prog

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

R-Programming - Ai&ds 10 Prog

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

WEEK 10

Introduction to Non-Tabular Data Types: Time series, spatial data, Network data. Data
Transformations: Converting Numeric Variables into Factors, Date Operations, String Parsing.

NON-TABULAR DATA TYPES:

Time Series Data:


Time series data is a sequence of data points collected at specific time intervals.
It is commonly used in various fields such as finance, economics, and environmental science to
analyze trends and patterns over time.
In R, time series data can be represented using specialized classes like ts (time series), xts (extensible
time series), and zoo (z's ordered observations).
Spatial Data:
Spatial data represents information about the geographic location and attributes of real-world
features such as points, lines, and polygons.
It is used in fields like geography, environmental science, and urban planning for mapping,
visualization, and spatial analysis.
In R, spatial data is typically handled using packages like sp, sf, and raster which provide classes and
functions for working with spatial objects and performing spatial operations.
Network Data:
Network data represents relationships between entities in a system, where entities are represented
as nodes and relationships as edges.
It is used in fields like social network analysis, transportation planning, and computer networks to
analyze connectivity and network properties.
In R, network data can be represented using packages like igraph, network, and ggraph which
provide classes and functions for creating, visualizing, and analyzing networks.

DATA TRANSFORMATIONS:

Converting Numeric Variables into Factors:


AIM: To Write an R program for Converting Numeric Variables into Factors.

FUNCTIONS REQUIRED:

c():
The c() function is used to concatenate or combine values into a single vector. It stands for
"combine" or "concatenate". In the provided code, it's used to create a numeric vector by combining
individual numeric values.
as.factor():
The as.factor() function is used to convert a vector into a factor. Factors in R are used to represent
categorical data. The function assigns a numeric code to each unique value in the vector and stores
these codes as integers. It's useful when you want to work with categorical data in R.
cat():
The cat() function is used to concatenate and print values to the console. It stands for "concatenate
and print". It is typically used for simple concatenation and printing without any formatting. In the
provided code, it's used to print messages to the console.
print():
The print() function is used to print the specified object to the console or to a file. It's a generic
function in R used for printing various types of R objects. In the provided code, it's used to print the
original numeric vector and the converted factor vector to the console.

SOURCE CODE:

# Create a numeric vector


numeric_vector<- c(1, 2, 3, 1, 2, 3, 1, 2, 3)

# Convert numeric vector into a factor


factor_vector<- as.factor(numeric_vector)

# Print the original numeric vector


cat("Original numeric vector:\n")
print(numeric_vector)

# Print the converted factor vector


cat("\nConverted factor vector:\n")
print(factor_vector)

OUTPUT:

Original numeric vector:


[1] 1 2 3 1 2 3 1 2 3

Converted factor vector:


[1] 1 2 3 1 2 3 1 2 3
Levels: 1 2 3

Date Operations:
AIM:To Write an R program for Date Operations.

FUNCTIONS REQUIRED:

c():
The c() function is used to concatenate or combine values into a single vector. It stands for
"combine" or "concatenate". In the provided code, it's used to create a numeric vector by combining
individual numeric values.
Sys.Date():
The Sys.Date() function is used to obtain the current system date in R. It returns a Date object
representing the current date.
print():
The print() function is a generic function in R used to print its argument. It is used to display the
output on the console. When applied to a Date object, it prints the date in a human-readable
format.
format():
The format() function is used to format dates and times in R. It takes a Date object as input and
converts it into a character string according to the specified format. Commonly used format codes
include %Y for year, %m for month, %d for day, etc.
as.Date():
The as.Date() function is used to convert character data into Date objects in R. It is often used to
convert strings representing dates into Date objects so that date arithmetic and other operations
can be performed on them.

SOURCE CODE:

# Create a Date object representing today's date


today<- Sys.Date()

# Print today's date


cat("Today's date:\n")
print(today)

# Extract components of the date (year, month, day)


cat("\nComponents of today's date:\n")
cat("Year:", format(today, "%Y"), "\n")
cat("Month:", format(today, "%m"), "\n")
cat("Day:", format(today, "%d"), "\n")

# Add/subtract days from a date


cat("\nDate operations:\n")
cat("Yesterday's date:", format(today - 1, "%Y-%m-%d"), "\n")
cat("Tomorrow's date:", format(today + 1, "%Y-%m-%d"), "\n")

# Calculate the difference between two dates


date1 <- as.Date("2024-01-01")
date2 <- as.Date("2024-04-15")
difference<- date2 - date1
cat("\nDifference between two dates:", difference, "days\n")

OUTPUT:

Today's date:
[1] "2024-04-16"

Components of today's date:


Year: 2024
Month: 04
Day: 16

Date operations:
Yesterday's date: 2024-04-15
Tomorrow's date: 2024-04-17

Difference between two dates: 105 days


String Parsing:
AIM:To Write an R program for string parsing.

FUNCTIONS REQUIRED:

c():
The c() function is used to concatenate or combine values into a single vector. It stands for
"combine" or "concatenate". In the provided code, it's used to create a numeric vector by combining
individual numeric values.
print():
The print() function is a generic function in R used to print its argument. It is used to display the
output on the console. When applied to a Date object, it prints the date in a human-readable
format.
strsplit():
The strsplit() function is used to split a character vector (or a string) into substrings based on a
specified delimiter or regular expression pattern.
It takes two main arguments: the character vector or string to split, and the delimiter or regular
expression pattern used for splitting.
The function returns a list where each element contains the substrings resulting from the split
operation.
grep():
The grep() function is used to search for matches of a specified pattern within a character vector or
string.
It takes several arguments, including the pattern to search for, the character vector or string to
search within, and optional parameters controlling the matching process.
When value = TRUE is specified, grep() returns the actual elements of the character vector that
match the pattern, rather than their indices.
If value = FALSE (the default), grep() returns the indices of matching elements within the vector.

SOURCE CODE:

# Sample string
text<- "The quick brown fox jumps over the lazy dog."

# Split the string into words


words<- strsplit(text, "\\s+")[[1]]

# Print the words


cat("Words in the string:\n")
print(words)

# Extract words containing 'o'


o_words<- grep("o", words, value = TRUE)

# Print words containing 'o'


cat("\nWords containing 'o':\n")
print(o_words)

# Extract words ending with 's'


s_words<- grep("s$", words, value = TRUE)
# Print words ending with 's'
cat("\nWords ending with 's':\n")
print(s_words)

OUTPUT:

.Words in the string:


[1] "The" "quick" "brown" "fox" "jumps" "over" "the" "lazy" "dog."

Words containing 'o':


[1] "brown" "fox" "over" "dog."

Words ending with 's':


[1] "jumps" "dogs"

You might also like