Suppose I have the element
x <- "1:4"
I try to convert x into a sequence as follow
as.integer(x)
[1] NA
Warning message:
NAs introduced by coercion
How can I get an integer sequence beginning from a string?
Suppose I have the element
x <- "1:4"
I try to convert x into a sequence as follow
as.integer(x)
[1] NA
Warning message:
NAs introduced by coercion
How can I get an integer sequence beginning from a string?
When you type x <- "1:4"
you are not creating a string of 4 characters 1, 2, 3, and 4. You are creating a string that's simply "1:4". Naturally when you convert this to numbers, it makes no sense. Hence you want x <- 1:4
.
EDIT: Gaurav beat me to this.
Firstly, x<-"1:4"
will be saved as "1:4"
x<-"1:4"
str(x)
chr "1:4"
Therefore it cannot be turned into an integer, it is merely saved as characters. You first need to remove the "" from the x<-"1:4" at which point you'll by default get a list of 4 integers 1:4 :
x<-1:4
str(x)
int [1:4] 1 2 3 4