我正在尝试使用R将数据帧写入Teradata。数据框的格式很宽(超过100列),写入Teradata意味着声明每个变量的名称和类。请注意,以下数据仅作为示例。
iris$integerRandom <- seq_along(iris$Sepal.Length)
iris$Dates <- seq.Date(as.Date("2018-01-01"), by = "d", length.out = nrow(iris))
iris$Dates2 <- seq.Date(as.Date("2019-01-01"), by = "d", length.out = nrow(iris))
iris$Species <- as.character(iris$Species)
iris$characterRandom <- sample(letters, nrow(iris), replace = TRUE)
## Getting Numeric and Integer Names first
names_num <- names(iris)[which(sapply(iris, class) %in% c("integer", "numeric"))]
names_date <- names(iris)[which(sapply(iris, class) %in% "Date")]
names_character <- names(iris)[which(sapply(iris, class) %in% "character")]
## Generating variable names with corresponding variable types
paste(gsub("varchar(300)", '"varchar(300)"', gsub(",", " = varchar(300), ", toString(names_character))), "varchar(300)", sep = " = ")
paste(gsub(",", " = date", toString(names_date)), " = date")
paste(gsub("varchar(300)", '"float"', gsub(",", " = float, ", toString(names_num))), "float", sep = " = ")
理想情况下,我想要所需的输出说 Species =“ varchar(300)”,characterRandom =“ varchar(300)”,依此类推。请注意,变量在声明Teradata(在这种情况下为SQL)的名称和类型时很重要,因为顺序很重要,代码可能对两种工具都适用。因此,变量名称的顺序以及 Sepal.Length并以characterRandom结尾。