R is a programming language and free software environment for statistical computing and graphics supported by the R Foundation for Statistical Computing. It is widely used among statisticians and data miners for data analysis and developing statistical software.
R Anti-Patterns Overview
Growing Objects in a Loop
Using apply() on Data Frames
apply()
on data frames as it converts them to matrices, which can lead to unexpected results if columns have different types. Use lapply()
, vapply()
, or packages like dplyr instead.Using $ and [[]] Inconsistently
[[]]
when the column name is stored in a variable or when writing functions that take column names as parameters.Using attach()
attach()
as it can lead to confusing scoping issues and hard-to-find bugs. Use with()
, explicit references, or pipe operators instead.Using row.names
row.names()
to store actual data. Instead, include the information as a proper column in your data frame.Using for Loops Instead of Vectorization
Using global() and <<-
<<-
operator. Instead, pass values explicitly to functions and return modified values.Not Using Proper Error Handling
tryCatch()
to gracefully handle errors and provide meaningful error messages.Using T and F Instead of TRUE and FALSE
T
and F
as shortcuts for TRUE
and FALSE
. They are just variables that can be reassigned, potentially leading to confusing bugs.Using stringsAsFactors=TRUE
stringsAsFactors = FALSE
.Using rm(list=ls()) to Clean Environment
rm(list=ls())
to clean your environment, especially in scripts or functions. It can lead to unexpected behavior and makes code less reproducible. Instead, restart R or use separate R sessions.Not Using Proper Package Management
Using setwd() in Scripts
setwd()
in scripts as it makes them less portable. Use the here
package or relative paths instead.Not Using Proper Documentation
Using sapply() When Value Type Might Vary
sapply()
when the return type might vary. Use vapply()
with an explicit return type or lapply()
instead.Using Inefficient Data Structures
Not Using Proper Subsetting
Not Using Proper Naming Conventions
Not Using Proper Testing
Using print() for Debugging
print()
statements for debugging. Use proper logging packages like logger or futile.logger instead.Not Using Proper Memory Management
Using Default Arguments Incorrectly