I need to convert multiple columns (all of the values in each column) in a data frame to have NA
as their value, is this possible in R?
Asked
Active
Viewed 28 times
-1
-
you can do `df[, your_cols] <- NA` – Maël Sep 05 '22 at 15:57
-
`as.data.frame(matrix(NA, 10, 5))` will create a data frame with 10 rows, 5 columns with NAs. – dcarlson Sep 05 '22 at 15:58
1 Answers
0
You may assign NA
to multiple columns. Example:
mtcars[c("mpg", "cyl", "disp")] <- NA
head(mtcars)
# mpg cyl disp hp drat wt qsec vs am gear carb
# Mazda RX4 NA NA NA 110 3.90 2.620 16.46 0 1 4 4
# Mazda RX4 Wag NA NA NA 110 3.90 2.875 17.02 0 1 4 4
# Datsun 710 NA NA NA 93 3.85 2.320 18.61 1 1 4 1
# Hornet 4 Drive NA NA NA 110 3.08 3.215 19.44 1 0 3 1
# Hornet Sportabout NA NA NA 175 3.15 3.440 17.02 0 0 3 2
# Valiant NA NA NA 105 2.76 3.460 20.22 1 0 3 1

jay.sf
- 60,139
- 8
- 53
- 110
-
Thank you so much! I had a feeling it was a simple answer but for some reason everything I found was showing me how to replace NA! – Gab Sep 06 '22 at 16:06