老问题,但是,怎么样
as.na <- function(obj){
if(is.factor(obj)){
# Special case for factors - any others that need to be handled?
factor(rep(NA, length(obj)), levels = levels(obj))
} else{
objClass <- class(obj)
x <- rep(NA, length(obj))
class(x) <- objClass
x
}
}
对于数据框:
DF <- data.frame(
int = seq(1, 10),
real = seq(1, 10) + 0.1,
char = letters[1:10],
logi = rep(c(TRUE, FALSE), 5),
Date = seq.Date(as.Date("2019-09-03"), by = 1, length.out = 10),
posix = seq.POSIXt(as.POSIXct("2019-09-03 12:00:00"), by = 360, length.out = 10),
stringsAsFactors = FALSE
)
DF$factr <- as.factor(LETTERS[1:10])
str(DF)
'data.frame': 10 obs. of 7 variables:
$ int : int 1 2 3 4 5 6 7 8 9 10
$ real : num 1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1 10.1
$ char : chr "a" "b" "c" "d" ...
$ logi : logi TRUE FALSE TRUE FALSE TRUE FALSE ...
$ Date : Date, format: "2019-09-03" "2019-09-04" "2019-09-05" ...
$ posix: POSIXct, format: "2019-09-03 12:00:00" "2019-09-03 12:06:00" "2019-09-03 12:12:00" ...
$ factr: Factor w/ 10 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10
DF_na <- DF
for(i in colnames(DF_na)){
DF_na[,i] <- as.na(DF_na[,i])
}
str(DF_na)
'data.frame': 10 obs. of 7 variables:
$ int : int NA NA NA NA NA NA NA NA NA NA
$ real : num NA NA NA NA NA NA NA NA NA NA
$ char : chr NA NA NA NA ...
$ logi : logi NA NA NA NA NA NA ...
$ Date : Date, format: NA NA NA ...
$ posix: POSIXct, format: NA NA NA ...
$ factr: Factor w/ 10 levels "A","B","C","D",..: NA NA NA NA NA NA NA NA NA NA
对于数据表:
library(data.table)
DT <- data.table::data.table(
int = seq(1, 10),
real = seq(1, 10) + 0.1,
char = letters[1:10],
logi = rep(c(TRUE, FALSE), 5),
Date = seq.Date(as.Date("2019-09-03"), by = 1, length.out = 10),
posix = seq.POSIXt(as.POSIXct("2019-09-03 12:00:00"), by = 360, length.out = 10),
factr = as.factor(LETTERS[1:10])
)
str(DT)
Classes ‘data.table’ and 'data.frame': 10 obs. of 7 variables:
$ int : int 1 2 3 4 5 6 7 8 9 10
$ real : num 1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1 10.1
$ char : chr "a" "b" "c" "d" ...
$ logi : logi TRUE FALSE TRUE FALSE TRUE FALSE ...
$ Date : Date, format: "2019-09-03" "2019-09-04" "2019-09-05" ...
$ posix: POSIXct, format: "2019-09-03 12:00:00" "2019-09-03 12:06:00" "2019-09-03 12:12:00" ...
$ factr: Factor w/ 10 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10
- attr(*, ".internal.selfref")=<externalptr>
DT_na <-
copy(DT)[, lapply(.SD, as.na)]
str(DT_na)
Classes ‘data.table’ and 'data.frame': 10 obs. of 7 variables:
$ int : int NA NA NA NA NA NA NA NA NA NA
$ real : num NA NA NA NA NA NA NA NA NA NA
$ char : chr NA NA NA NA ...
$ logi : logi NA NA NA NA NA NA ...
$ Date : Date, format: NA NA NA ...
$ posix: POSIXct, format: NA NA NA ...
$ factr: Factor w/ 10 levels "A","B","C","D",..: NA NA NA NA NA NA NA NA NA NA
- attr(*, ".internal.selfref")=<externalptr>