努力使用 read_tsv() 代替 read.csv()
Struggling to use read_tsv() in place of read.csv()
已回答:非常感谢 Bob,请注意问题未指定 comment='#'。为什么这有效,当 'skip' 应该跳过违规行仍然是一个谜。另请参阅 Gray 的评论:Excel 的 'Text to Columns' 非 R 解决方案的功能。
大家好,
多年来,这一直是我背上的恶魔。
我使用的数据始终是制表符分隔的 .txt 文件的集合,因此我的分析总是从收集每个文件的文件路径并将其输入 read.csv() 并绑定到 df 开始。
dat <- list.files(
path = 'data',
pattern = '*.txt',
full.names = TRUE,
recursive = TRUE
) %>%
map_df( ~read.csv( ., sep='\t', skip=16) ) # actual data begins at line 16
这正是我想要的,但过去几年我一直在过渡到 tidyverse。
我不介意使用 utils::read.csv(),因为我的数据集通常很小,所以不会感受到 readr 的速度优势。但是,为了保持一致性,我宁愿使用 readr。
当我做同样的事情,但是子readr::read_tsv(),即
dat <-
.... same call to list.files()
%>%
map_df( ~read_tsv( ., skip=16 ))
我总是得到一个空的 (0x0) table。但它似乎是 'reading' 数据,因为我的数据中的每一列都收到 'Parsed with column specification: cols()' 的警告打印。
显然我在这里误会了,但我不知道我不明白的是什么,这让我的寻找答案充满挑战和无果。
所以...我做错了什么?
提前致谢!
编辑:请求了我的(其中一个)数据文件的示例片段,希望格式正确!
# KLIBS INFO
# > KLibs Commit: 11a7f8331ba14052bba91009694f06ae9e1cdd3d
#
# EXPERIMENT SETTINGS
# > Trials Per Block: 72
# > Blocks Per Experiment: 8
#
# SYSTEM INFO
# > Operating System: macOS 10.13.4
# > Python Version: 2.7.15
#
# DISPLAY INFO
# > Screen Size: 21.5" diagonal
# > Resolution: 1920x1080 @ 60Hz
# > View Distance: 57 cm
PID search_type stimulus_type present_absent response rt error
3 time COLOUR present absent 5457.863881 TRUE
3 time COLOUR absent absent 5357.009108 FALSE
3 time COLOUR present present 2870.76412 FALSE
3 time COLOUR absent absent 5391.404728 FALSE
3 time COLOUR present present 2686.6131 FALSE
3 time COLOUR absent absent 5306.652878 FALSE
编辑:使用 Jukob 的建议
files <- list.files(
path = 'data',
pattern = '*.txt',
full.names = TRUE,
recursive = TRUE
)
for (i in 1:length(files)) {
print(read_tsv(files[i], skip=16))
}
打印:
Parsed with column specification:
cols()
# A tibble: 0 x 0
... for each file
如果我打印文件,我会得到正确的文件路径列表。如果我删除 skip=16 我得到:
Parsed with column specification:
cols(
`# KLIBS INFO` = col_character()
)
Warning: 617 parsing failures.
row col expected actual file
15 -- 1 columns 21 columns 'data/raw/2019/colour/p1.2019-02-28.txt'
16 -- 1 columns 21 columns 'data/raw/2019/colour/p1.2019-02-28.txt'
17 -- 1 columns 21 columns 'data/raw/2019/colour/p1.2019-02-28.txt'
18 -- 1 columns 21 columns 'data/raw/2019/colour/p1.2019-02-28.txt'
19 -- 1 columns 21 columns 'data/raw/2019/colour/p1.2019-02-28.txt'
... ... ......... .......... ........................................
See problems(...) for more details.
... for each file
你可以试试下面的代码吗? i 的值可能会让您了解哪个文件有问题。
files <- list.files(path = "path", full.names = T, pattern = ".csv")
for (i in 1:length(files)){
print(read_tsv(files[i], skip = 16))
}
FWIW 通过执行以下操作,我能够使用您的代码段解决问题:
# Didn't work for me since when I copy and paste your snippet,
# the tabs become spaces, but I think in your original file
# the tabs are preserved so this should work for you
read_tsv("dat.tsv", comment = "#")
# This works for my case
read_table2("dat.tsv", comment = "#")
甚至不需要指定 skip
参数!
而且,不知道为什么使用 skip
而不是 comment
会失败...:(
已回答:非常感谢 Bob,请注意问题未指定 comment='#'。为什么这有效,当 'skip' 应该跳过违规行仍然是一个谜。另请参阅 Gray 的评论:Excel 的 'Text to Columns' 非 R 解决方案的功能。
大家好,
多年来,这一直是我背上的恶魔。
我使用的数据始终是制表符分隔的 .txt 文件的集合,因此我的分析总是从收集每个文件的文件路径并将其输入 read.csv() 并绑定到 df 开始。
dat <- list.files(
path = 'data',
pattern = '*.txt',
full.names = TRUE,
recursive = TRUE
) %>%
map_df( ~read.csv( ., sep='\t', skip=16) ) # actual data begins at line 16
这正是我想要的,但过去几年我一直在过渡到 tidyverse。
我不介意使用 utils::read.csv(),因为我的数据集通常很小,所以不会感受到 readr 的速度优势。但是,为了保持一致性,我宁愿使用 readr。
当我做同样的事情,但是子readr::read_tsv(),即
dat <-
.... same call to list.files()
%>%
map_df( ~read_tsv( ., skip=16 ))
我总是得到一个空的 (0x0) table。但它似乎是 'reading' 数据,因为我的数据中的每一列都收到 'Parsed with column specification: cols()' 的警告打印。
显然我在这里误会了,但我不知道我不明白的是什么,这让我的寻找答案充满挑战和无果。
所以...我做错了什么?
提前致谢!
编辑:请求了我的(其中一个)数据文件的示例片段,希望格式正确!
# KLIBS INFO
# > KLibs Commit: 11a7f8331ba14052bba91009694f06ae9e1cdd3d
#
# EXPERIMENT SETTINGS
# > Trials Per Block: 72
# > Blocks Per Experiment: 8
#
# SYSTEM INFO
# > Operating System: macOS 10.13.4
# > Python Version: 2.7.15
#
# DISPLAY INFO
# > Screen Size: 21.5" diagonal
# > Resolution: 1920x1080 @ 60Hz
# > View Distance: 57 cm
PID search_type stimulus_type present_absent response rt error
3 time COLOUR present absent 5457.863881 TRUE
3 time COLOUR absent absent 5357.009108 FALSE
3 time COLOUR present present 2870.76412 FALSE
3 time COLOUR absent absent 5391.404728 FALSE
3 time COLOUR present present 2686.6131 FALSE
3 time COLOUR absent absent 5306.652878 FALSE
编辑:使用 Jukob 的建议
files <- list.files(
path = 'data',
pattern = '*.txt',
full.names = TRUE,
recursive = TRUE
)
for (i in 1:length(files)) {
print(read_tsv(files[i], skip=16))
}
打印:
Parsed with column specification:
cols()
# A tibble: 0 x 0
... for each file
如果我打印文件,我会得到正确的文件路径列表。如果我删除 skip=16 我得到:
Parsed with column specification:
cols(
`# KLIBS INFO` = col_character()
)
Warning: 617 parsing failures.
row col expected actual file
15 -- 1 columns 21 columns 'data/raw/2019/colour/p1.2019-02-28.txt'
16 -- 1 columns 21 columns 'data/raw/2019/colour/p1.2019-02-28.txt'
17 -- 1 columns 21 columns 'data/raw/2019/colour/p1.2019-02-28.txt'
18 -- 1 columns 21 columns 'data/raw/2019/colour/p1.2019-02-28.txt'
19 -- 1 columns 21 columns 'data/raw/2019/colour/p1.2019-02-28.txt'
... ... ......... .......... ........................................
See problems(...) for more details.
... for each file
你可以试试下面的代码吗? i 的值可能会让您了解哪个文件有问题。
files <- list.files(path = "path", full.names = T, pattern = ".csv")
for (i in 1:length(files)){
print(read_tsv(files[i], skip = 16))
}
FWIW 通过执行以下操作,我能够使用您的代码段解决问题:
# Didn't work for me since when I copy and paste your snippet,
# the tabs become spaces, but I think in your original file
# the tabs are preserved so this should work for you
read_tsv("dat.tsv", comment = "#")
# This works for my case
read_table2("dat.tsv", comment = "#")
甚至不需要指定 skip
参数!
而且,不知道为什么使用 skip
而不是 comment
会失败...:(