As an alternative to Zheyuan Li's simpler method that works really well with math operations, this is a little more generalized for operations that might be a little more complex:
cols_to_change <- c(2,4,5,8,10)
# or
cols_to_change <- c("AM01_01", "AM01_02", ...)
myfun <- function(z) z-1
ba_data[cols_to_change] <- lapply(ba_data[cols_to_change], myfun)
Walk-through:
lapply(L, F)
iterates the function F
over each "element" in L
(a list). In R, a data.frame
is mostly just a list
where each element (column) is the same length.
- Because
lapply(..)
returns a list
, and the columns you're working on are likely a subset of the entire frame, we need to assign it back to the respective columns; ergo ba_data[cols_to_change] <-
The reason this is more general and can be useful: if your operation is more of a "lookup" than a "subtract one", you can change myfun
to be more specific. For instance, if in all of these columns you need to replace 1
with 21
, 2
with 97
, and 3
with -1
, and leave all other values intact, then you might write the function as:
myfun <- function(z, lookup) {
for (nm in names(lookup)) {
z <- ifelse(as.character(z) == nm, lookup[[nm]], z)
}
z
}
ba_data[cols_to_change] <-
lapply(ba_data[cols_to_change],
function(x) myfun(x, c("1"=21, "2"=97, "3"=-1)))
If you were to use a lookup like this, realize that I named them as strings regardless of what class the original data is, because "names" of things in R should not start with (or be entirely) numbers.