Reputation: 769
How do I change all values in a dataset to NA or 0, while preserving all column names and ideally, preserving an identical data structure.
Upvotes: 0
Views: 972
Reputation: 887831
We can just do assignment while using []
to preserve the attributes
df1[] <- 0
Or for NA
df1[] <- NA
-testing
> df1 <- head(iris, 3)
> df1[] <- 0
> df1
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0 0 0
Upvotes: 4