Calculate - First

From Q
Jump to: navigation, search

Extracts the first rows or columns in selected outputs

Extracts the first rows or columns (e.g. first 2 rows or first 5 columns) of selected summary tables, crosstabs or R vectors, R matrices, R arrays, R data frames, R lists or R tables, and saves them as a new R outputs. Alternatively, if the input data is labeled with dates or date-times, this feature is able to extract the first date or time periods in the data, e.g., first 3 years, first week, first 10 hours etc. A new R output is created for each selected output and this R output contains controls which allow settings to be specified (see Options section below).

Examples

Shown below is the output from extracting the first 3 rows from a summary table of a Pick One question for age:

First rows.PNG

Shown below is the output from extracting the first 1 calendar month from an R vector that is labeled with dates starting from November 20, 2020. If instead a non-calendar period was specified (see Calendar periods in the Options section below), all dates up to and including December 19, 2020 would be extracted.

First month2.PNG

Options

Data The data output from which rows or columns are to be extracted. This can be a summary table, crosstab or R vector, R matrix, R array, R data frame, R list or R table.

Unit Specify whether to extract first rows, columns, or date-time periods (year, quarter, month, week, day, hour, minute, second) from the data. If the data is one-dimensional, "Row" should be selected for this option.

Keep The number of units (specified in the previous control) to extract.

Calendar periods Whether to consider calendar periods. When calendar periods are considered, a year is counted as the 12-month period from January to December; a quarter is one of the following 3-month periods: January-March, April-June, July-September, October-December; a month is the period from the first to last day in the 12 months in the calendar; a week is the 7 day period from Sunday to Saturday; a day is the 24 hour period from midnight to midnight; and hours, minutes and seconds are the fixed periods as indicated on a clock. For example, if the first 2 calendar years are to be kept and the earliest date in the data is Nov 6, 2020, all data labeled in 2020 and 2021 will be kept. When non-calendar periods are considered instead, all periods are compared against a reference date-time, which is the earliest date-time in the labels. For periods that do not have a fixed duration, i.e., years, quarters and months, a period is defined using the day and month of the reference date-time. For example if the reference date-time is Nov 6 2020 12:34:56pm, and the first 2 years of data are to be kept, all data up to (but not including) Nov 6 2022 12:34:56pm would be retained. If the reference date-time falls on a day for which the corresponding end-of-period date-time does not exist, then all actual date-times up to this mock end-of-period date-time are considered. For example, 1 month from March 31 12:34:56pm includes all date-times up to and including April 30.

Code

includeWeb("QScript Functions for First and Last");
firstLast(true);

See Also