View source: R/transform_data.R
| transform_data | R Documentation |
Replaces NA, Inf, or zero values in specified columns of a data frame with a user-defined value or the column mean.
transform_data(data, feature, data_type = c("NA", "Inf", "zero"), into = 0)
data |
Data frame. Input data to be transformed. |
feature |
Character vector. Column names in 'data' to apply transformation. |
data_type |
Character. Type of value to replace: '"NA"', '"Inf"', or '"zero"'. |
into |
Value to replace specified type with. Default is 0. If '"mean"', replaces with column mean (excluding NA/Inf values as appropriate). |
Data frame with specified transformations applied to selected features.
Dongqiang Zeng
data_matrix <- data.frame(
A = c(1, 2, NA, 4, Inf),
B = c(Inf, 2, 3, 4, 5),
C = c(0, 0, 0, 1, 2)
)
# Replace NAs with 0
transform_data(data_matrix, feature = c("A", "B"), data_type = "NA")
# Replace Inf values with the mean of the column
transform_data(data_matrix,
feature = c("A", "B"),
data_type = "Inf", into = "mean"
)
# Replace zeros with -1 in column C
transform_data(data_matrix, feature = "C", data_type = "zero", into = -1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.