R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
In R, data conversion between different types is common. Here's a tutorial that provides an overview of various data conversion functions in R:
x <- 123.45 y <- as.character(x) # "123.45" z <- as.numeric(y) # 123.45
TRUE
; zero is FALSE
.x <- 1 y <- as.logical(x) # TRUE
x <- factor(c("apple", "banana", "cherry")) y <- as.character(x) # "apple" "banana" "cherry"
mylist <- list(name = c("John", "Anna"), age = c(25, 30)) df <- as.data.frame(mylist) mydf <- data.frame(name = c("John", "Anna"), age = c(25, 30)) lst <- as.list(mydf)
mymatrix <- matrix(1:6, nrow=2) df <- as.data.frame(mymatrix) mydf <- data.frame(a = 1:3, b = 4:6) mat <- as.matrix(mydf)
date_str <- "2023-09-05" date <- as.Date(date_str) datetime_str <- "2023-09-05 14:30:00" datetime <- as.POSIXct(datetime_str, format="%Y-%m-%d %H:%M:%S")
mat <- matrix(1:6, nrow=2) vec <- as.vector(mat)
x <- "Hello" is.character(x) # TRUE
Practice these conversions in different scenarios to become more proficient with them.
as.numeric() Function in R:
Use as.numeric()
to convert objects to numeric.
# as.numeric() function in R x <- "123" numeric_x <- as.numeric(x)
Convert Character to Numeric in R:
Convert character data to numeric using as.numeric()
.
# Convert character to numeric in R character_value <- "42.5" numeric_value <- as.numeric(character_value)
as.character() Function in R:
Use as.character()
to convert objects to character.
# as.character() function in R y <- 42.5 character_y <- as.character(y)
Converting Factors to Characters in R:
Convert factors to characters using as.character()
.
# Converting factors to characters in R factor_data <- factor(c("A", "B", "C")) character_data <- as.character(factor_data)
as.factor() Function in R:
Use as.factor()
to convert objects to factors.
# as.factor() function in R numeric_vector <- c(1, 2, 3) factor_vector <- as.factor(numeric_vector)
Convert Numeric to Factor in R:
Convert numeric data to factor using as.factor()
.
# Convert numeric to factor in R numeric_data <- c(1, 2, 3, 1, 2) factor_data <- as.factor(numeric_data)
as.logical() Function in R:
Use as.logical()
to convert objects to logical.
# as.logical() function in R logical_value <- as.logical(1)
Convert Date to Character in R:
Convert date data to character using as.character()
.
# Convert date to character in R date_value <- as.Date("2023-01-01") character_date <- as.character(date_value)
Conversion Functions for Lists and Data Frames in R:
Utilize various conversion functions for lists and data frames.
# Conversion functions for lists and data frames in R list_data <- list(1, "abc", TRUE) data_frame <- data.frame(A = c(1, 2), B = c("x", "y")) list_to_vector <- unlist(list_data) data_frame_to_matrix <- as.matrix(data_frame)