You can do:
x[, 1:2][is.na(x[, 1:2])] <- 0
or better (IMHO), use the variable names:
x[c("a", "b")][is.na(x[c("a", "b")])] <- 0
In both cases, 1:2
or c("a", "b")
can be replaced by a pre-defined vector.
More Related Contents:
- Replace in a factor column
- Aggregate / summarize multiple variables per group (e.g. sum, mean)
- How do I replace NA values with zeros in an R dataframe?
- Replacing character values with NA in a data frame
- Assign multiple columns using := in data.table, by group
- Omit rows containing specific column of NA
- Count NAs per row in dataframe [duplicate]
- Subset of rows containing NA (missing) values in a chosen column of a data frame
- Match values in data frame with values in another data frame and replace former with a corresponding pattern from the other data frame
- Remove columns from dataframe where some of values are NA
- How to subset data in R without losing NA rows?
- How to delete columns that contain ONLY NAs?
- Merge rows in a dataframe where the rows are disjoint and contain NAs
- Replace logical values (TRUE / FALSE) with numeric (1 / 0)
- Randomly insert NAs into dataframe proportionaly
- R: Replace multiple values in multiple columns of dataframes with NA
- Select last non-NA value in a row, by row
- Replace a value NA with the value from another column in R
- Cleaning `Inf` values from an R dataframe
- Replace empty values with value from other column in a dataframe
- write.csv for large data.table
- Select NA in a data.table in R
- Replace all NA with FALSE in selected columns in R
- DT[!(x == .)] and DT[x != .] treat NA in x inconsistently
- Merge overlapping ranges into unique groups, in dataframe
- Convert a data frame to a data.table without copy
- Find columns with all missing values
- R: replacing NAs in a data.frame with values in the same position in another dataframe
- Move NAs to the end of each column in a data frame
- What you can do with a data.frame that you can’t with a data.table?