0

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?

BillyLeZeurbé
  • 57
  • 1
  • 10

2 Answers2

0

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.

Gaurav Bansal
  • 5,221
  • 14
  • 45
  • 91
  • 1
    here the reason of my question https://stackoverflow.com/questions/41595942/convert-list-of-list-to-a-coerced-dataframe?noredirect=1#comment70395772_41595942 – BillyLeZeurbé Jan 11 '17 at 23:40
0

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
Travis Gaddie
  • 152
  • 10