na by column r
Sure! Here is an example of how you can replace missing values (NA) by column in R:
# Create a sample data frame
df <- data.frame(
A = c(1, 2, NA, 4, 5),
B = c(6, NA, 8, 9, 10),
C = c(11, 12, 13, NA, 15)
)
# Replace NA values by column
df[] <- lapply(df, function(x) {
x[is.na(x)] <- mean(x, na.rm = TRUE)
x
})
Explanation:
We start by creating a sample data frame called
df
using thedata.frame()
function. This data frame has three columns: A, B, and C.The next step is to replace the NA values in each column. We use the
lapply()
function to apply a function to each column of the data frame.Inside the
lapply()
function, we use an anonymous function (defined usingfunction(x)
) to replace the NA values in each column. Theis.na()
function is used to identify the NA values in the column.We then use the
mean()
function to calculate the mean of the values in each column, excluding the NA values (na.rm = TRUE
).Finally, we assign the calculated mean values back to the NA values in each column using the assignment operator
<-
. This updates the original data frame with the replaced values.
By following these steps, you can replace missing values by column in R. Let me know if you have any further questions.