我有一个大数据集,包含约94列和300万行。该文件在列之间使用单个或多个空格作为分隔符。我需要在R中从该文件中读取一些列。为此,我尝试使用read.table()函数,并使用下面代码中的选项,请参见以下代码-
### Defining the columns to be read from the file, the first 5 column, then we do not read next 24, after this we read next 5 columns. Last 60 columns are not read in-
col_classes = c(rep("character",2), rep("numeric", 3), rep("NULL",24), rep("numeric", 5), rep("NULL", 60))
### Reading first 100 rows of the data
data <- read.table(file, sep = " ",header = F, nrows = 100, na.strings ="", stringsAsFactors= F)
由于要读取的文件有多个空格作为某些列之间的分隔符,上述方法无法使用。是否有使用其他方法可以高效读取此文件的方式。
sep=" "
参数。read.table
默认知道如何处理多个空格。 - Hong Ooiread.table/read.csv
的默认分隔符是sep="", 这意味着“多个空格”,我们本应该期望它应该是正则表达式"\w*"或"\w+"而不是""。 - smci