library(tidystata)27 Basic expressions
The practical chapters introduce expressions where they are needed: arithmetic in Chapter 14, conditions in Chapter 6, missingness in Chapter 10, and text conversion in Chapter 16. This optional chapter looks more closely at functions as objects, automatic conversion within vectors, and additional numerical operations.
These ideas provide foundations for writing functions and iterating over inputs. You can use the practical chapters without reading this chapter first. Load tidystata for var_type(), which identifies an object’s type:
27.1 Objects and types
The examples in Section 1.2 have already introduced several R objects. We can use var_type() from tidystata to identify an object’s type. The results below are "double" for ordinary numbers, including numbers with a decimal part, "character" for text, and "function" for an operation we can call. R stores even the whole number 25 as a double when we write it this way.
The name sqrt refers to the function itself; sqrt(25) calls it and returns the number 5. Therefore, var_type(sqrt) identifies a function, while var_type(sqrt(25)) identifies the numeric result of that call. The package setup above makes var_type() available.
# A real number (double)
var_type(25)[1] "double"
# Text (character)
var_type("Hello, R!")[1] "character"
# A function (function)
var_type(sqrt)[1] "function"
Parentheses distinguish a function call, such as sqrt(25), from the function’s name on its own.
27.2 A shared vector type
An atomic vector, the kind used for this chapter’s simple values, has one basic type. c() combines its inputs in their written order. Mixing numbers and text converts the numbers to text:
c(12, "hello")[1] "12" "hello"
Both entries print with quotation marks. The number has become "12", while the other string remains "hello".
An explicit conversion such as as.numeric() is explained and checked in Section 12.4. Automatic conversion happens while constructing the vector, so inspect its type when combining different inputs.
27.3 Optional reference
Table 27.1 gives additional operations to consult when a calculation needs them.
| Operation | Purpose | Example | Result |
|---|---|---|---|
abs() |
Distance from zero | abs(-3) |
3 |
floor() |
Round down to an integer | floor(2.7) |
2 |
ceiling() |
Round up to an integer | ceiling(2.7) |
3 |
%/% |
Integer quotient | 17 %/% 5 |
3 |
%% |
Remainder | 17 %% 5 |
2 |
Dividing 17 by 5 gives three complete groups of 5 and a remainder of 2: 17 = 3 * 5 + 2. For negative inputs, “down” means toward a smaller number: floor(-2.7) gives -3, while ceiling(-2.7) gives -2. At exact halfway values, round() uses the even last retained digit: round(2.5) is 2 and round(3.5) is 4. Decimal representation can affect borderline cases.
27.4 Locate a questionnaire item
Integer division and remainders help interpret repeated numbering patterns. Consider a questionnaire layout in which Q21 through Q52 form four consecutive scales, each containing eight items. This is a numbering exercise; it supplies no respondent answers. Which scale contains Q40, and where does that question fall within it?
Subtract 21 to count positions from zero. Each complete block of eight advances one scale; the remainder counts positions within that block. Add one to express both results using the questionnaire’s numbering, which starts at one:
question <- 40
offset <- question - 21
question_scale <- offset %/% 8 + 1
question_item <- offset %% 8 + 1
question_scale[1] 3
question_item[1] 4
The offset is 19: two complete blocks of eight and three further positions. Q40 therefore belongs to scale 3, item 4. The calculation depends on the stated layout: consecutive question numbers, equal scale lengths, and Q21 as the first item. Check the questionnaire before applying it to an actual instrument. Numbers outside Q21-Q52 do not belong to these four scales, even though R could still calculate a quotient and remainder for them.
27.5 Review
- A function’s name refers to the function object; parentheses call it with inputs.
- An atomic vector has one basic type; combining numbers and text converts the numbers to text.
abs(),floor(), andceiling()provide additional numerical operations.- Integer division and remainders describe complete groups and what remains.
Chapter 28 develops vector construction, indexing, column selection, and the structures that functions receive and return.
27.6 Exercises
- For the layout in Section 27.4, locate Q21, Q28, Q29, and Q52. Explain the change between Q28 and Q29. Reconstruct each original question number from its scale and item to check the calculation.
Worked answers
c() combines the four question numbers in their written order. The arithmetic applies to each entry:
answer_questions <- c(21, 28, 29, 52)
answer_offsets <- answer_questions - 21
answer_scales <- answer_offsets %/% 8 + 1
answer_items <- answer_offsets %% 8 + 1
answer_scales[1] 1 1 2 4
answer_items[1] 1 8 1 8
answer_reconstructed <- 21 + (answer_scales - 1) * 8 + (answer_items - 1)
answer_reconstructed[1] 21 28 29 52
The scales are 1, 1, 2, 4, and the item positions are 1, 8, 1, 8. Q28 finishes the first scale; Q29 begins the second. Reconstruction starts at Q21, advances eight positions per completed scale, then advances within the current scale. It returns the four original numbers, including both endpoints of the layout.