我正在努力将我的文本文件转换为R中的表。问题是列是由"#"但是当我将read.table()
函数与sep="#"
一起使用时,它仍然看起来不像普通表并返回大量错误。
我如何解决这个问题?
文本文件如下所示:
Hotel Van Walsum#121800#3#4.2#70#0.7 miles to City centre?#
Bilderberg Parkhotel Rotterdam#124547#4#3.7#83#0.5 miles to City centre?#
NH Atlanta Rotterdam#208643#4#3.9#55#0.3 miles to City centre?#
Nhow Rotterdam#439534#4#4.3#83#1.3 miles to City centre?#
The Manhattan Hotel Rotterdam#173187#5#4.5#101#0.1 miles to City centre?#
Mainport#427255#5#4.7#112#0.8 miles to City centre?#
easyHotel Rotterdam City Centre#438095#2#3.8#49#0.5 miles to City centre?#
ss Rotterdam Hotel & Restaurants#277518#4#4.3#84#1.8 miles to City centre?#
H2otel#276462#3#4.1#67#0.7 miles to City centre?#
Van der Valk Hotel Rotterdam-Blijdorp#272914#4#4.1#71#1.4 miles to City centre?#
NH Capelle#139024#4#4.1#55#4.5 miles to City centre?#
The Student Hotel Rotterdam#434163#3#4.2#62#1.3 miles to City centre?#
Novotel Rotterdam Brainpark#124868#4#4.4#69#2.4 miles to City centre?#
Hotel Van Walsum#121800#3#4.2#70#0.7 miles to City centre?#
Hampshire Hotel - Savoy Rotterdam#115253#4#3.7#65#0.8 miles to City centre?#
Delta Hotel Rotterdam#179050#4#4.2#80#5.8 miles to City centre?#
Novotel Rotterdam Schiedam#144828#4#4.3#69#4.2 miles to City centre?#
Hotel Rotterdam#209923#4#3.7#74#0.4 miles to City centre?#
答案 0 :(得分:5)
我们需要在read.table
中指定字段分隔符和注释字符:
read.table("myFile.txt", sep = "#", comment.char = "")
答案 1 :(得分:5)
您可以使用其他comment.char
来加载它,该文件不会出现在您的文件中,例如#
这有效:
df <- read.table(text='Hotel Van Walsum#121800#3#4.2#70#0.7 miles to City centre?#
Bilderberg Parkhotel Rotterdam#124547#4#3.7#83#0.5 miles to City centre?#
NH Atlanta Rotterdam#208643#4#3.9#55#0.3 miles to City centre?#
Nhow Rotterdam#439534#4#4.3#83#1.3 miles to City centre?#
The Manhattan Hotel Rotterdam#173187#5#4.5#101#0.1 miles to City centre?#
Mainport#427255#5#4.7#112#0.8 miles to City centre?#
easyHotel Rotterdam City Centre#438095#2#3.8#49#0.5 miles to City centre?#
ss Rotterdam Hotel & Restaurants#277518#4#4.3#84#1.8 miles to City centre?#
H2otel#276462#3#4.1#67#0.7 miles to City centre?#
Van der Valk Hotel Rotterdam-Blijdorp#272914#4#4.1#71#1.4 miles to City centre?#
NH Capelle#139024#4#4.1#55#4.5 miles to City centre?#
The Student Hotel Rotterdam#434163#3#4.2#62#1.3 miles to City centre?#
Novotel Rotterdam Brainpark#124868#4#4.4#69#2.4 miles to City centre?#
Hotel Van Walsum#121800#3#4.2#70#0.7 miles to City centre?#
Hampshire Hotel - Savoy Rotterdam#115253#4#3.7#65#0.8 miles to City centre?#
Delta Hotel Rotterdam#179050#4#4.2#80#5.8 miles to City centre?#
Novotel Rotterdam Schiedam#144828#4#4.3#69#4.2 miles to City centre?#
Hotel Rotterdam#209923#4#3.7#74#0.4 miles to City centre?#', sep='#', comment.char='*')
输出:
df
V1 V2 V3 V4 V5 V6 V7
1 Hotel Van Walsum 121800 3 4.2 70 0.7 miles to City centre? NA
2 Bilderberg Parkhotel Rotterdam 124547 4 3.7 83 0.5 miles to City centre? NA
3 NH Atlanta Rotterdam 208643 4 3.9 55 0.3 miles to City centre? NA
4 Nhow Rotterdam 439534 4 4.3 83 1.3 miles to City centre? NA
5 The Manhattan Hotel Rotterdam 173187 5 4.5 101 0.1 miles to City centre? NA
6 Mainport 427255 5 4.7 112 0.8 miles to City centre? NA
7 easyHotel Rotterdam City Centre 438095 2 3.8 49 0.5 miles to City centre? NA
8 ss Rotterdam Hotel & Restaurants 277518 4 4.3 84 1.8 miles to City centre? NA
9 H2otel 276462 3 4.1 67 0.7 miles to City centre? NA
10 Van der Valk Hotel Rotterdam-Blijdorp 272914 4 4.1 71 1.4 miles to City centre? NA
11 NH Capelle 139024 4 4.1 55 4.5 miles to City centre? NA
12 The Student Hotel Rotterdam 434163 3 4.2 62 1.3 miles to City centre? NA
13 Novotel Rotterdam Brainpark 124868 4 4.4 69 2.4 miles to City centre? NA
14 Hotel Van Walsum 121800 3 4.2 70 0.7 miles to City centre? NA
15 Hampshire Hotel - Savoy Rotterdam 115253 4 3.7 65 0.8 miles to City centre? NA
16 Delta Hotel Rotterdam 179050 4 4.2 80 5.8 miles to City centre? NA
17 Novotel Rotterdam Schiedam 144828 4 4.3 69 4.2 miles to City centre? NA
18 Hotel Rotterdam 209923 4 3.7 74 0.4 miles to City centre? NA
答案 2 :(得分:3)
read.table
,read.csv
,read.delim
它们都用于读取文件并转换为数据框,并相互之间有一些微妙的变化。
read.table(file, header = FALSE, sep = "", quote = "\"'",
dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"),
row.names, col.names, as.is = !stringsAsFactors,
na.strings = "NA", colClasses = NA, nrows = -1,
skip = 0, check.names = TRUE, fill = !blank.lines.skip,
strip.white = FALSE, blank.lines.skip = TRUE,
comment.char = "#",
allowEscapes = FALSE, flush = FALSE,
stringsAsFactors = default.stringsAsFactors(),
fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)
read.table
默认为comment.char="#"
,这意味着在您的文字中可以有#
表示的评论。要按任何字符阅读csv文件或分隔文件,请使用read.csv
或read.delim
,其中没有默认comment.char
,只需指定sep
个字符。
默认read.csv
是
read.csv(file, header = TRUE, sep = "#", quote = "\"",
dec = ".", fill = TRUE, comment.char = "", ...)
您可以使用
read.csv(file, sep="#",header=F)