R Anti-Patterns Overview
R Anti-Patterns Overview
Growing Objects in a Loop
Growing Objects in a Loop
Using apply() on Data Frames
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
Using $ and [[]] Inconsistently
[[]]
when the column name is stored in a variable or when writing functions that take column names as parameters.Using attach()
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
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 for Loops Instead of Vectorization
Using global() and <<-
Using global() and <<-
<<-
operator. Instead, pass values explicitly to functions and return modified values.Not Using Proper Error Handling
Not Using Proper Error Handling
tryCatch()
to gracefully handle errors and provide meaningful error messages.Using T and F Instead of TRUE and FALSE
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
Using stringsAsFactors=TRUE
stringsAsFactors = FALSE
.Using rm(list=ls()) to Clean Environment
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
Not Using Proper Package Management
Using setwd() in Scripts
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
Not Using Proper Documentation
Using sapply() When Value Type Might Vary
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
Using Inefficient Data Structures
Not Using Proper Subsetting
Not Using Proper Subsetting
Not Using Proper Naming Conventions
Not Using Proper Naming Conventions
Not Using Proper Testing
Not Using Proper Testing
Using print() for Debugging
Using print() for Debugging
print()
statements for debugging. Use proper logging packages like logger or futile.logger instead.Not Using Proper Memory Management
Not Using Proper Memory Management
Using Default Arguments Incorrectly
Using Default Arguments Incorrectly