0

如何重命名具有“X 或 X.1 或 X.3”值的列标题,但它应该使用下一列的标题引用和重命名。

代码:

library(pdftools)
library(data.table)
library(tabulizer)
pdf_file <- "new.pdf"

out2 <- extract_tables(pdf_file, pages =c(89), output = "data.frame")
out2<-as.data.table(out2)
colnames(out2)

实际输出:

"G" "X" "Day.7" "X.1"   "Day.8" "X.2"   "Day.9" "X.3"  

预期输出:

"G" "Day.7 "Day.7" "Day.8"   "Day.8" "Day.9"  "Day.9"
4

1 回答 1

0

如果不访问您正在使用的 PDF,很难知道,但这可能是因为创建一个 2 列共享相同名称的数据框不是默认行为。如果您确定您知道列是什么,那么您可以简单地手动更改名称。

    # Make fake data that matches what the pdf extract_tables function might be doing
    originalnames <- c("G", "X", "Day.7", "X.1", "Day.8", "X.2", "Day.9", "X.3")
    mm <- matrix(data=NA, nrow=1, ncol=8)
    colnames(mm) <- originalnames
    
    # Create a data frame with required names
    df <- data.frame(mm)
    requirednames <- c("G", "X", "Day.7", "Day.7", "Day.8",   "Day.8", "Day.9",  "Day.9")
    names(df) <- requirednames
于 2021-01-21T13:25:11.030 回答