Data Types in R
Data types in R determine how values are stored and which operations make sense. In business analytics, a price, customer identifier, membership category, and payment flag can look similar on screen while representing different kinds of information. Choosing an appropriate representation helps you preserve identifiers, perform valid calculations, and recognize missing information before interpreting results.
Match storage to meaning
An object's storage type describes its underlying representation in R. The four atomic types you will commonly use for business values are logical, integer, double, and character. An atomic vector contains elements of one storage type; even a single value is a vector of length one.
Storage type | Example | Suitable business use |
|---|---|---|
Logical | TRUE | Whether an invoice has been paid |
Integer | 6L | A whole-number item count |
Double | 18.75 | An amount in dollars |
Character | "0042" | An identifier whose leading zeros matter |
The suffix L creates an integer literal. Without it, a number such as 6 normally has double storage, despite having no fractional part. Double storage accommodates decimal values but does not represent every decimal fraction exactly. R also has complex and raw atomic types, used for complex numbers and bytes respectively; these are less common in routine business analysis.
Quotation marks matter: "18.75" is text, whereas 18.75 is a number. Likewise, TRUE is logical, while "TRUE" is character. Choose based on meaning: an account identifier generally remains character even if every character is a digit.
Separate storage type from class
Use typeof() to inspect underlying storage and class() to inspect the object's class, which helps determine how R interprets and handles it.
amount <- 18.75
typeof(amount) # "double"
class(amount) # "numeric"Here, "numeric" is the class name returned for an ordinary double vector. It does not indicate a different underlying storage type. Also, is.numeric() returns TRUE for ordinary integer and double vectors.
A factor represents categorical values through integer codes and a set of labels called levels:
segment <- factor(c("Business", "Personal"),
levels = c("Personal", "Business"))
typeof(segment) # "integer"
class(segment) # "factor"
levels(segment) # "Personal" "Business"Those integers identify categories; they are not measured quantities. An ordered factor additionally records a meaningful ranking. For example, membership tiers can use factor(tiers, levels = c("Bronze", "Silver", "Gold"), ordered = TRUE). The order does not establish equal distances between tiers.
Predict conversions and missing values
Coercion is conversion from one type to another. When c() combines ordinary logical, integer, double, and character values into an atomic vector, it chooses a common type. Among these types, the progression is logical, integer, double, then character.
typeof(c(6L, 2.5)) # "double"
typeof(c(18.75, "due")) # "character"In numeric coercion, TRUE becomes 1 and FALSE becomes 0. Explicit conversion uses functions such as as.numeric(), as.integer(), and as.character(). Check whether conversion preserves the original meaning.
as.numeric(c("16.40", "pending"))
# Returns 16.4 and NA, with a warningNA represents a missing value. It is distinct from zero, an empty string, and the literal text "NA". Use is.na(x) to test for missingness; x == NA does not provide a reliable missing-value test. Missing values can occur within different storage types, including integer, double, and character vectors.
What to remember
- Choose types according to business meaning, not appearance.
- Use typeof() for storage and class() for interpretation.
- An atomic vector has one common storage type.
- Factor codes represent levels, not measured amounts.
- Check conversion warnings and test missingness with is.na().
Example: Checking types in a retailer's export
A retailer receives three transaction records. Customer identifiers must retain their leading zeros. Invoice amounts are recorded in dollars, and the text "unreported" explicitly means that the amount is unknown. Payment confirmation may also be unknown.
customer_id <- c("0073", "0196", "0284")
invoice_text <- c("84.50", "126.00", "unreported")
paid <- c(TRUE, FALSE, NA)
channel <- factor(c("Store", "Online", "Store"),
levels = c("Online", "Store"))For each part, predict the result before revealing the solution.
a) Identify the current storage types of customer_id, invoice_text, and paid. Explain which representations should be retained and which should change before calculating with invoice amounts.
Quotation marks make both the identifiers and the invoice entries character values. The unquoted payment flags, together with the missing logical value, form a logical vector.
typeof(customer_id) # "character"
typeof(invoice_text) # "character"
typeof(paid) # "logical"Retain character storage for customer identifiers because their leading zeros matter. Retain logical storage for payment confirmation: TRUE means confirmed, FALSE means not confirmed, and NA means unknown. Convert invoice amounts to numeric values before doing arithmetic; their present character representation contains text rather than quantities.
b) Convert invoice_text to a numeric vector. State the resulting amounts, explain the warning, and check which entry is missing.
Apply explicit numeric conversion while keeping the original vector available for comparison.
invoice <- as.numeric(invoice_text)
# Warning: NAs introduced by coercion
# Values: 84.5 126.0 NA
typeof(invoice)
# "double"The numeric-looking strings become 84.50 dollars and 126.00 dollars. R cannot interpret "unreported" as a number, so it produces NA and warns that conversion introduced missing information. Here, the supplied business meaning confirms that this entry is unknown.
Check the positions with is.na():
is.na(invoice)
# FALSE FALSE TRUEThe third invoice amount is missing. It is not zero dollars. The original text vector remains available if you need to inspect what caused the warning.
c) Predict typeof(channel), class(channel), and as.numeric(channel). Then recover the channel names as character values.
A factor stores integer codes together with category levels. The explicit level order assigns code 1 to "Online" and code 2 to "Store".
The observed sequence is ________, ________, Store, so its code sequence is 2, 1, 2:
typeof(channel)
# "integer"
class(channel)
# "factor"
as.numeric(channel)
# 2 1 2These numbers identify categories; they do not measure channel performance or transaction amounts. Retrieve the labels with character conversion:
as.character(channel)
# "Store" "Online" "Store"The result is a character vector containing the original channel names in transaction order. The factor itself remains suitable when you want to represent the channels as categories.
A business analyst is checking unfamiliar R objects before using them in a report. Match each function call to the information it returns about an object named x.
The object's classification, which can influence how functions handle it
A logical answer indicating whether the object has the type used for TRUE, FALSE, and unknown truth values
A logical answer indicating whether the object contains text values in the corresponding atomic type
The internal storage category, such as double or character
is.character(x)
class(x)
typeof(x)
is.logical(x)
An analyst accidentally combines a payment flag, an order count, and a delivery duration in days into one atomic vector:
mixed <- c(TRUE, 11L, 4.25)Which description correctly predicts the storage type and values of mixed?