R: [read_delim()] - 得到一个警告,如何摆脱它?

R: [read_delim()] - Getting a warning, how to get rid of it?

我是 R 的新手。我有一个代码可以从没有 headers 的文件中读取并从每行中选择前两个元素。每条线确定一条飞机航线。第一个元素描述起飞的机场名称,第二个元素描述降落的机场名称。

来自文件数千行之一的结构:

LFPO;LFSL;00;AT45;210;LFPO;LFSL;189930747;150907;1815;!!!!;HOP25ZZ;HOP;0;HOP25ZZ-LFPO-LFSL-20150907180500;N;0;;;245346;;;150907;1805;0;;X;;;;;;;;;;210;;0;20150907175900;AA45458325;;;;;NEXE;NEXE;;;;;20150907180500;;;;245346;;;;;;;;;;;;;;;;;;;;;;;;;;;;HOP;;;;;;;;;;;0

我的代码所做的是从所有机场中,根据机场的总起降次数排名前十,这是根据机场在此期间的降落和起飞总数计算得出的文件中指定的时间。

代码有效,我正在尝试使用更好的库或函数来改进它。到目前为止,我将 read.table() 函数替换为 read_delim() ,这大大缩短了处理时间。尽管如此,我还是在打印所需结果时收到了一些警告。

如何消除警告或如何整理警告?

这是代码,这里是 link 到测试文件 https://easyupload.io/4lw4o4:

start_time <- Sys.time()
# Libraries
library(compare)
library(janitor)
library(data.table)
library(readr)[enter image description here][1]


# START
args = commandArgs(trailingOnly=TRUE)
# test if there is at least one argument
if (length(args)==0) {
  fileName  = "traffic1week.exp2"
} else if (length(args)==1) {
  # default output file
  fileName = args[1]
}

# Convert file to dataframe
df = read_delim(fileName, delim = ";", col_names =F)

# Read file
names(df) = c("Airport","Airport")

# Retrieve 1st column
origin = df[1]# takeOff Airports
# Retrieve 2nd column
destination = df[2] # Landing Airports

# Number of movements
takeOff_airports = unlist(table(origin)) 
landing_airports = unlist(table(destination))

# Convert to dataframes
df1 = as.data.frame(takeOff_airports)
names(df1) = c('Airport', 'TakeOffs')
df2 = as.data.frame(landing_airports)
names(df2) = c('Airport', 'Landings')

# Merge both dataframes
df3 = merge(df1, df2, all=T)

# Sum colum[3] values from each dataframe
df3$Total_Movements = df3$TakeOffs+df3$Landings

# Orde by max total movements
df3 = df3[order(-df3$Total_Movements),]

# Reorganize columns
result = df3[, c(1, 4, 2, 3)]

# Print results 
print(result[1:10,], row.names = FALSE)

# STOP
end_time = Sys.time()
cat(paste("Processing time: ", end_time - start_time),sep="\n\n")

这是警告:

Attaching package: ‘compare’

The following object is masked from ‘package:base’:

isTRUE


Attaching package: ‘janitor’

The following objects are masked from ‘package:stats’:

chisq.test, fisher.test

Parsed with column specification:
cols(
.default = col_logical(),
X1 = col_character(),
X2 = col_character(),
X3 = col_character(),
X4 = col_character(),
X5 = col_character(),
X6 = col_character(),
X7 = col_character(),
X8 = col_double(),
X9 = col_double(),
X10 = col_character(),
X11 = col_character(),
X12 = col_character(),
X13 = col_character(),
X14 = col_double(),
X15 = col_character(),
X16 = col_character(),
X17 = col_double(),
X20 = col_double(),
X23 = col_double(),
X24 = col_character()
# ... with 13 more columns
)
See spec(...) for full column specifications.
Warning message:
The `names` must have length 95, not 2.
This warning is displayed once per session. 
Airport Total_Movements TakeOffs Landings
LFPG            9407     4926     4481
EHAM            9399     4879     4520
LTBA            9384     4749     4635
EGLL            9057     4749     4308
EDDF            8930     4624     4306
EDDM            7535     3816     3719
LEMD            7412     3789     3623
LIRF            6957     3528     3429
LEBL            6406     3221     3185
EGKK            5995     3050     2945
Processing time:  1.78606390953064

我只想得到:

Airport Total_Movements TakeOffs Landings
LFPG            9407     4926     4481
EHAM            9399     4879     4520
LTBA            9384     4749     4635
EGLL            9057     4749     4308
EDDF            8930     4624     4306
EDDM            7535     3816     3719
LEMD            7412     3789     3623
LIRF            6957     3528     3429
LEBL            6406     3221     3185
EGKK            5995     3050     2945
Processing time:  1.78606390953064

加载时包发出的警告是正常的。它们只是提供信息。有一种方法可以通过使用 suppressMessages() 来抑制消息,但是如果您完全抑制程序中的每条消息,则在出现异常时可能会丢失一些重要消息。

试试这个

suppressMessages(library(compare))

至于read_delim()给出的警告,它们只是告诉你每列假设的class类型,因为你没有自己指定它们。如果将 colClasses 参数传递给 read_delim(),它将停止 babbling。您也可以通过将 read_delim() 放在 suppressMessage()suppressWarnings() 中来抑制这些消息,如上所述。或者,如果您改用 read.table(),它会安静地为列假定合理的类型,而不会发出有关它的消息。