What are the basic data types in R?
a) Numeric, Integer, Character, Logical, Complex
b) String, Boolean, Array, Set, Dictionary
c) Byte, Short, Long, Double, Float
d) Object, Array, List, Tuple, Set
Answer:
a) Numeric, Integer, Character, Logical, Complex
Explanation:
R supports several basic data types, including:
- Numeric: Represents real numbers (e.g.,
3.14
). - Integer: Represents integer values (e.g.,
2L
). - Character: Represents strings of text (e.g.,
"Hello"
). - Logical: Represents Boolean values (
TRUE
orFALSE
). - Complex: Represents complex numbers (e.g.,
1+2i
).
# Examples of R data types
num <- 42.5 # Numeric
int <- 2L # Integer
char <- "Hello, R!" # Character
bool <- TRUE # Logical
comp <- 1 + 2i # Complex
print(class(num)) # Output: "numeric"
print(class(int)) # Output: "integer"
print(class(char)) # Output: "character"
print(class(bool)) # Output: "logical"
print(class(comp)) # Output: "complex"
In this example, variables of different data types are created, and their types are printed using the class()
function.
Understanding these data types is crucial for data manipulation and analysis in R, as they determine how data is stored, processed, and interpreted in your programs.