Возможно, это не самый элегантный способ, но мне кажется, что он работает.
#install.packages("tidyverse")
#install.packages("dataCompareR")
library("tidyverse")
library("dataCompareR")
# create reproducible df
set.seed(123)
df1 <- data.frame(
A = as.numeric(1:10),
B = sample(seq(as.POSIXct('2000/01/01', tz = "UTC"), as.POSIXct('2018/01/01', tz = "UTC"), by="day"), size=10),
C = as.numeric(sample(20:90, size = 10)),
D = sample(c("yes", "no"), size=10, replace = TRUE),
E = as.factor(sample(1000:2000, size = 10)),
F = as.character(c("test","test2","test3","test4","test5","test6","test7","test8","test9","test10")),
G = as.factor(c("test","test2","test3","test4","test5","test6","test7","test8","test9","test10")),
H = as.character(sample(seq(as.POSIXct('2000/01/01', tz = "UTC"), as.POSIXct('2018/01/01', tz = "UTC"), by="day"), size=10)),stringsAsFactors=FALSE
)
df1 #look at df
unlist(lapply(df1, class)) #look at df classes
df1_clean <- df1 %>% mutate_all(funs(type.convert(as.character(.), as.is = TRUE))) #reassign classes by running type.convert (input are all variables from the df but as.character)
unlist(lapply(df1_clean, class)) #look at df classes now
#check if a column is a Date - https://stackoverflow.com/questions/18178451/is-there-a-way-to-check-if-a-column-is-a-date-in-r
tmp=sapply(df1_clean, function(x) !all(is.na(as.Date(as.character(x),format="%Y-%m-%d", tz = "UTC"))))
# if tmp is True, change according column to as.POSIXct
for (i in 1:ncol(df1_clean)){
if (tmp[i] == T){
df1_clean[,i]<- as.POSIXct(df1_clean[,i], tz = "UTC")
}
}
df1_clean #look at df
unlist(lapply(df1_clean, class)) #look at df classes
comp <- rCompare(df1, df1_clean) #compare your dfs before and after using the dataCompareR package
summary(comp) # check summary