回答道:“非常感谢鲍勃,”他说,“问题是没有指定注释=‘#’。”为什么这样做,什么时候“跳过”应该跳过冒犯行仍然是一个谜。还请参阅Gray的评论see :Excel的非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)表。但它似乎是在“读取”数据,因为我得到了对数据中每一列的“用列规范解析: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编辑:使用胡科布的建议
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发布于 2020-08-10 19:34:12
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会失败.:(
发布于 2020-08-10 17:51:12
你能试着遵循代码吗?我的价值可能会让你知道哪个文件有问题。
files <- list.files(path = "path", full.names = T, pattern = ".csv")
for (i in 1:length(files)){
print(read_tsv(files[i], skip = 16))
}https://stackoverflow.com/questions/63344998
复制相似问题