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:

  1. We start by creating a sample data frame called df using the data.frame() function. This data frame has three columns: A, B, and C.

  2. 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.

  3. Inside the lapply() function, we use an anonymous function (defined using function(x)) to replace the NA values in each column. The is.na() function is used to identify the NA values in the column.

  4. We then use the mean() function to calculate the mean of the values in each column, excluding the NA values (na.rm = TRUE).

  5. 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.