r - 如何读取缩进的数据?
问题描述
我有一个制表符分隔的文件,如下所示:
ID trait1 trait2 trait3
1111 1 1 0
2222 1 0 0
3333 0 0 1
4444 0 1 0
请注意,数据行相对于列标题缩进。
我尝试使用这种语法读取文件并保留列名:
df <- fread("/path/to/file", sep="\t", data.table=FALSE, header=TRUE)
结果如下所示:
df
V1 1111 1 1 0
1 NA 2222 1 0 0
2 NA 3333 0 0 1
3 NA 4444 0 1 0
我试过strip.white = TRUE
了,但这没有帮助。
解决方案
read_table2
从readr
也可以工作
readr::read_table2(
"ID trait1 trait2 trait3
1111 1 1 0
2222 1 0 0
3333 0 0 1
4444 0 1 0
"
)
#> # A tibble: 4 x 4
#> ID trait1 trait2 trait3
#> <dbl> <dbl> <dbl> <dbl>
#> 1 1111 1 1 0
#> 2 2222 1 0 0
#> 3 3333 0 0 1
#> 4 4444 0 1 0
由reprex 包(v0.3.0)于 2019 年 9 月 17 日创建
推荐阅读
- puppet - 在模块中,使用定义的资源在 puppet3 中有效,但在 puppet5 中失败
- c# - 无法更新 Wcf 引用
- javascript - 将日期从星期几转换为日期名称
- r - 如何使用 sapply 保留所有变量?
- css - Electron 中 SVG 组的变换原点被忽略
- javascript - 如何迭代多个页面的 API?
- angular-material-5 - 禁用控件状态时,角度材质输入将占位符移动到顶部
- python - 在 TensorFlow Estimator API 中设置分段学习率
- ember.js - 如何初始化 Ember 模型 ID?
- javascript - 如何在 React Native 中将 flexbox 用于文本组件?水平环绕,垂直增量