0

I have recently started learning R language and was working on combination of vectors. I was following a tutorial and when I try to print character, complex, integer vector in c() there is a space difference between them.

I have enclosed the snapshot for the same as I might not be able to articulate it properly in words. R Vector Output

Heroka
  • 12,889
  • 1
  • 28
  • 38
  • 1
    A vector can only contain one data type, in your case character strings. Anyway, why are you not using R.exe or Rgui (or preferably an IDE such as RStudio)? See, e.g., http://stackoverflow.com/a/3415135/1412059 – Roland Jan 25 '16 at 12:06
  • Anyway, I can reproduce with `x <- c(123.56, 21, "rajat", 2+4i); print(x)`. I don't know why this happens, but is this really a problem? – Roland Jan 25 '16 at 12:07
  • Hey Roland, thanks for the reply. I read about the difference between vectors, list, arrays after I posted this question.I came to understand. Its not creating any issue, I was just curious about it. – R internet P Jan 25 '16 at 13:51

1 Answers1

1

As Roland commented, a vector can only contain one specific data type. Here since you have character datatype, all the other data types are coerced into character datatype.

x <- c(123.56, 21, "rajat", 2+4i); print(x)

The space which should not be a problem as far as I understand is created because you have different number of characters in each elements of the vector.

>nchar(x) 
[1] 6 2 5 4

Now, if you have equal number of characters the space distribution is as expected:

x <- c(123.56, 210000, "rajata", 2+442i); print(x)
[1] "123.56" "210000" "rajata" "2+442i"

nchar(x)
[1] 6 6 6 6
Community
  • 1
  • 1
discipulus
  • 2,665
  • 3
  • 34
  • 51