r - 将表前后元数据的.txt文件导入R
问题描述
我正在尝试将 .txt 文件导入 R,但遇到了一些困难。我的文件在文件中包含实际数据表之前和之后的元数据。我如何告诉 read.table() 函数来解决这个问题?我已经尝试过跳过参数,但没有成功。如果有人能给我指出一个好的资源的方向,那就太好了!我不能仅从文档中到达那里。
这是我的 .txt 文件的 Google Drive 链接:https ://drive.google.com/file/d/1VG3fBso0s15NGeHnN32CK68bylxrB06t/view?usp=sharing
解决方案
data <- read.table('hackr.txt',header = T, sep =';', skip=22, stringsAsFactors = FALSE)
# Optional: cn <- colnames(data)
tmp <- as.data.frame(stringr::str_split_fixed(data[,1], "\t",n=Inf))[2:nrow(data),]
我还可以为您清理(删除)一些垃圾列:
for (f in names(tmp)) {
if (length(unique(tmp[[f]])) == 1) {
cat(f, "is constant. I am deleting it.\n"); tmp[[f]] <- NULL
}}
head(tmp) V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 2018-05-28 20:38:34 00:00:29.9 32.1 32.1 -.- -.- -.- -.- 2018-05-28 20:38:34 00:00:29.8 32.7 32.4 -.- -.- -.- -.- 2018-05-28 20:38:34 00:00:29.7 31.9 32.2 -.- -.- -.- -.- 2018-05-28 20:38:34 00:00:29.6 34.9 33.1 -.- -.- -.- -.- 2018-05-28 20:38:34 00:00:29.5 70.6 63.6 -.- -.- -.- -.- 2018-05-28 20:38:34 00:00:29.4 70.4 65.7 -.- -.- -.- -.-
如果您想保留原始标题,只需运行可选(注释)行并使用相同类型的逻辑解析它们。它们的分隔符与表格的其余部分不同。
推荐阅读
- angular - 表单值在提交时没有得到安慰,没有发现错误
- python - Boto3 在使用 update_service_specific_credential 时获取 InvalidClientTokenId
- python - 将原始值拆分并映射到不同的熊猫列
- javascript - 检查是否在 Javascript isset 中设置了 2 个 php 变量
- angular - 使用“ng build --prod”部署应用程序时出错
- powerbi - Power BI 查询中的变量
- snakemake - “conda”命令不可用
- c - 当我要将 uint32 变量与 sint32 变量相乘时,为什么我会出错?
- mysql - 转换列中的行
- css - 如何实现输入的下划线效果 - css