0% found this document useful (0 votes)
3 views6 pages

Statistical Analysisi Program

The document provides an R program that takes user input for name and age, and demonstrates type conversion with examples. It includes code for creating and displaying matrices and lists of store items. Additionally, it explains type conversion functions in R, such as as.numeric() and as.character().

Uploaded by

dharmendra.gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Statistical Analysisi Program

The document provides an R program that takes user input for name and age, and demonstrates type conversion with examples. It includes code for creating and displaying matrices and lists of store items. Additionally, it explains type conversion functions in R, such as as.numeric() and as.character().

Uploaded by

dharmendra.gupta
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

write a R program to take input from the user (name and age) and display the

values

What Do You Mean by Type Conversion?


Type conversion refers to the process of converting a value from one data type
to another. In R, this is often necessary when working with data from different
sources or performing operations that require specific data types.
Examples of type conversion include:
 Converting a character string "123" to a numeric value 123.
 Converting a numeric value 1 to a logical value TRUE.
R provides several functions for type conversion, such as:
 [Link](): Converts to numeric.
 [Link](): Converts to character.
 [Link](): Converts to logical.
 [Link](): Converts to a factor.
# R program to demonstrate type conversion
# Original values of different types
char_value <- "456" # Character type
numeric_value <- 123 # Numeric type
logical_value <- TRUE # Logical type

# R program to demonstrate type conversion

# Initial values
char_value <- "123"
numeric_value <- 123
logical_value <- TRUE

# Convert character to numeric


converted_numeric <- [Link](char_value)
cat("Character to Numeric:", converted_numeric, "\n")

# Convert numeric to character


converted_char <- [Link](numeric_value)
cat("Numeric to Character:", converted_char, "\n")

# Convert logical to numeric


converted_numeric_from_logical <- [Link](logical_value)
cat("Logical to Numeric:", converted_numeric_from_logical, "\n")

# Convert numeric to logical


converted_logical <- [Link](numeric_value) # Non-zero becomes TRUE
cat("Numeric to Logical:", converted_logical, "\n")

# Attempting conversion of invalid types (e.g., non-numeric string to numeric)


invalid_conversion <- [Link]("abc")
cat("Invalid Conversion Result:", invalid_conversion, "\n") # Produces NA with a
warning

R program that creates vectors of different types (numeric, complex, logical, and
character) with a specified length of 10:
# R program to create vectors and combine them into a matrix
# Create three vectors with 3 integers each
a <- c(1, 2, 3)
b <- c(4, 5, 6)
c <- c(7, 8, 9)
# Combine the vectors into a 3x3 matrix
matrix_result <- cbind(a, b, c)

# Print the matrix


cat("The 3x3 matrix is:\n")
print(matrix_result)

abc
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
# R program to create matrices with labels and fill by rows or columns

# Create a 5x4 matrix, filled by rows


matrix_5x4 <- matrix(1:20, nrow = 5, ncol = 4, byrow = TRUE,
dimnames = list(paste("Row", 1:5), paste("Col", 1:4)))
cat("5x4 Matrix (filled by rows):\n")
print(matrix_5x4)

# Create a 3x3 matrix, filled by rows, with labels


matrix_3x3 <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE,
dimnames = list(paste("R", 1:3), paste("C", 1:3)))
cat("\n3x3 Matrix (filled by rows):\n")
print(matrix_3x3)

# Create a 2x2 matrix, filled by columns, with labels


matrix_2x2 <- matrix(1:4, nrow = 2, ncol = 2, byrow = FALSE,
dimnames = list(c("Row1", "Row2"), c("Col1", "Col2")))
cat("\n2x2 Matrix (filled by columns):\n")
print(matrix_2x2)

# R script to create and display a list object of store items

# Create a list of store items


store_items <- list(
Fruits = c("Orange", "Mango", "Apple", "Watermelon", "Banana"),
Juices = c("Appy", "Fruity", "Slice"),
Milkshakes = c("Mango", "Papaya", "Sapota")
)

# Display the list object


cat("List of store items:\n")
print(store_items)

You might also like