[英]Reading table from a crude text file in R
我有一个文本文件,前几行不需要,然后有一个像这样的表
- 连线 -
| col1 | col2 | col3 col4 col5 |
- 连线 -
| 1 | 2:24:21 PM 3/22/2012 | 0 0 1 |
| 2 | 2:24:21 PM 3/22/2012 | 1·0 |
题:
1.我可以使用扫描,但如何避免阅读“|” 和“ - ”?
2.我可以跳过前几行,但除了前几行之外,如何跳过说第50行。
您可以按原样将其读取为表格,然后将列拆分并重新组合。
txt <- "| col1 | col2 | col3 col4 col5 |
| 1 | 2:24:21 PM 3/22/2012 | 0 0 1 |
| 2 | 2:24:21 PM 3/22/2012 | 1 · 0 |"
x <- read.table(text = txt, sep = "|", header = TRUE, stringsAsFactors = FALSE)
## drop unnecessary columns from the original sep split
x <- x[,-c(1,ncol(x))]
## split the desired column by the spaces, result is a character matrix
## including an unnecessary first column
split.col3 <- do.call("rbind", strsplit(x[,3], " "))
## bind to the original, dropping the unneeded columns
cbind(x[,-3], split.col3[,-1])
col1 col2 1 2 3
1 1 2:24:21 PM 3/22/2012 0 0 1
2 2 2:24:21 PM 3/22/2012 1 · 0
我避免引用原始列名,因为你说你想跳过这些行。 只需将header = FALSE
和skip = 50
添加到read.table
调用,然后添加任何有意义的列名。
此外,您可以删除“。” 必要时从列中转换为日期时间格式或数字。 如果您colClasses
了解它们,请在read.table中使用colClasses
。 我有理由把它分解为许多步骤,而不是试图用一个读取函数来完成所有这些步骤。
这可以分三步完成。 (1)使用"|"
读入文件全部 作为分隔符,(2)创建一个只包含三列(包含为一个)的新文件,(3)然后使用空格分隔符读回这些列。 以下代码可以帮助您完成大部分工作。 可能需要的更改:文件名, V4
列名,以及导航到正确的目录( getwd/setwd
)。
a <- read.delim("a.txt", FALSE, sep="|")
write.table(a$V4, file="b.txt", quote=FALSE, row.names=FALSE, col.names=FALSE)
b <- read.delim("b.txt", FALSE, sep=" ")
合并a
和b
的相应列,然后就完成了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.