What happens when you combine multiple datatypes in an atomic vector in R programming language?

Viewed 62

I tried running a code to identify the type of the vector produced while combining different data types. Here is the code and what I got as the output. Can somebody explain why this output is seen?

v<-c(1L,2,TRUE) typeof(v)

Output: [1] "double"

2 Answers

An atomic vector can only hold values of a single data type. If you put several different types in it, these get coerced to a common type. In your case double.

IF you want to keep the data type of the original values, you need to use a list. Lists do not have this restriction.

Seems like this is the rule:

When you attempt to combine different types they will be coerced in a fixed order: character → double → integer → logical. For example, combining a character and an integer yields a character.

Related