Replaces all values with "NA" in R dataframe

Viewed 25

I have a set of data

x <- seq(1, 10, by=1)
y <- seq(1, 10, by=1)
data <- expand.grid(x,y)

I would like to create a new set of data called NA_data and replaces all the 100 values in that data frame with "NA"

1 Answers

Just multiply with NA as any operation with NA results in NA and here we could either multiply or add (+) or subtract (-) or divide (/) and it still returns NA

NA_data <- data * NA

Or another option is

NA_data <- data[rep(nrow(data) + 1, nrow(data)),]

Or use the matrix way

NA_data <- as.data.frame(matrix(nrow = nrow(data), 
        ncol = ncol(data), dimnames = list(NULL, names(data)))
Related