| f_long | R Documentation |
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.
f_long(
data,
measure_columns = NULL,
keep_cols = NULL,
category_name = "name",
value_name = "value",
category_labels = NULL,
...
)
data |
The input data frame (e.g., from |
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.
If NULL (default), the function will pivot ALL columns except those in |
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 |
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 |
... |
Additional arguments passed to |
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').
A "Tidy" data frame (tibble) of class f_long.
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.
# --- 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.