f_long: Transform 'Wide' (Excel) data to 'Long' (R) format

View source: R/flong.R

f_longR Documentation

Transform 'Wide' (Excel) data to 'Long' (R) format

Description

This function converts "wide" data (e.g. Excel tables) into a "long" list format. This is the essential first step to prepare your data for analysis and plotting in R.

Usage

f_long(
  data,
  measure_columns = NULL,
  keep_cols = NULL,
  category_name = "name",
  value_name = "value",
  category_labels = NULL,
  ...
)

Arguments

data

The input data frame (e.g., from read_excel).

measure_columns

(Optional) The columns containing your numeric measurements. These values are often the response variables, i.e. will end up on the Y-axis.

  • Use column names: c("OD_t0", "OD_t1")

  • Use column numbers: 2:5

  • Use helpers: starts_with("Measure")

If NULL (default), the function will pivot ALL columns except those in keep_cols.

keep_cols

(Optional) The columns that identify your samples (IDs). E.g., "SampleID", "PatientID", "Treatment", "Student number". These are repeated for every measurement. *If left empty, all non-measured columns are kept.* Important: If measure_columns is NULL, you MUST specify keep_cols.

category_name

Name for the new column containing the headers. Default is "name". Choose something logical like "Timepoints", "Genes", or "Condition".

value_name

Name for the new column containing the numbers. Default is "value". Choose something logical like "Absorbance", "Ct_Value", or "Weight".

category_labels

(Optional) A character vector of new, readable names for your categories, i.e. the measure_columns that you entered. Note: The order must match the order of measure_columns exactly. Useful for renaming "t0_raw" to "Start" instantly.

...

Additional arguments passed to tidyr::pivot_longer. E.g., values_drop_na = TRUE to remove empty cells immediately.

Details

Research data in Excel or output from lab instruments often contains measurements side-by-side (in columns). Many R functions require measurements in a single column (rows). 'f_long' performs this translation for you.

It performs three actions in one go: 1. Selects your measurement columns ('measure_columns'). 2. Keeps your important ID columns ('keep_cols') and removes the rest. 3. (Optional) Renames cryptic column headers into readable labels ('category_labels').

Value

A "Tidy" data frame (tibble) of class f_long.

Note

The custom class and attributes (f_long_value, f_long_category) are used by the plot and summary methods. Be aware that most dplyr or tidyr operations (e.g., filter, mutate) will silently strip these attributes. If that happens, use f_scan or f_summary directly with explicit column names instead.

Examples

# --- Example 1: Using the 'iris' dataset ---
# Scenario: The iris dataset looks clean, but it is actually "Wide".
# It has 4 columns of measurements side-by-side.
# To compare Sepal Length vs Width in a plot, we must stack them.

head(iris)

# Reshape: Combine Length and Width into one column and plot the data.
iris_long <- f_long(
  data = iris,
  measure_columns = c("Sepal.Length", "Sepal.Width"),
  keep_cols = "Species",
  category_name = "Sepal_Dimension",    # Describes the grouping (What did we measure?)
  value_name = "Size_cm",               # Describes the value (What is the number?)
  category_labels=c("Length", "Width")  # New category labels
  )

 head(iris_long)

 # Plot the data using f_scan
 plot(iris_long)

 # Make a f_summary table of iris_long
 summary(iris_long)

# --- Example 2: Using the 'airquality' dataset ---
# Scenario: Pivot daily measurements of Wind and Temperature over time.

head(airquality)

weather_long <- f_long(
  data = airquality,
  measure_columns = c("Wind", "Temp"),
  keep_cols = c("Month", "Day"),
  category_name = "Climate_Parameter", # Descriptive name
  value_name = "Reading_Value",        # Generic name (since units differ: mph vs F)
  values_drop_na = TRUE
)

head(weather_long)

rfriend documentation built on July 7, 2026, 1:06 a.m.