To convert a string to a numeric value (float) in R, you can use the 'as.numeric()' function.
In this example,
str1
with a value of "3.14"
.as.numeric()
function to convert the string to a float and store the result in a variable float1
.float1
.# Convert "3.14" String to Float
str1 <- "3.14"
float1 <- as.numeric(str1)
print(paste("Float value is:", float1))
[1] "Float value is: 3.14"
In this example,
str2
with a value of "10.5"
.as.numeric()
function to convert the string to a float and store the result in a variable float2
.float2
.# Convert "10.5" String to Float
str2 <- "10.5"
float2 <- as.numeric(str2)
print(paste("Float value is:", float2))
[1] "Float value is: 10.5"
In this tutorial, we learned How to convert String to Float in R language with well detailed examples.