【问题标题】:Pulling an disaggregated blocks of data Set into R将分解的数据块拉入 R
【发布时间】:2013-12-28 04:04:34
【问题描述】:

我有一个布局异常的 csv 文件。数据不是顶部的连续块。 csv文件可以这样描述:

Comment Strings
Empty row
Comment String

[Desired Data with 10 columns and an undetermined number of rows]

Empty Row

Comment String 

[Desired Data with 10 columns and an undetermined number of rows]

Empty Row

Comment String 

[包含 10 列且列数未定的所需数据]

.... and so on and so forth.

如前所述。每个数据块都有随机数量的行。

将这些数据提取到 R 中的最佳方法是什么? read.table/read.csv 只能做这么多。

 read.table("C:\\Users\\Riemmman\\Desktop\\Historical Data\\datafile.csv",header=F,sep=",",skip=15,blank.lines.skip=T)

【问题讨论】:

  • 请发布您的数据的小样本/示例。你想要的输出是什么?每个数据部分的前 10 列?虽然您可以在 R 中进行文本处理,但在 awk 或 sed 等其他实用程序中可以更快地完成。
  • “所需数据”块是否有通用标题?另外,我假设您的意思是“未确定的 行数”。
  • 是的,它们都具有相同的行数和相同的命名列。我的问题是告诉 R,“嘿.. 第一个连续块在这里,第二个在这里,跳过这部分,第三个在那里,等等。”
  • 有没有办法将整个内容加载到 R 中并在那里格式化?所以我会加载整个文件,其中包括数据和文本,然后在 R 中清理它。
  • @jessica,是的。 readLines 可以让你加载整个文件。请参阅my answer,了解 R 中的可能解决方案。

标签: regex r csv read.table


【解决方案1】:

您可以结合使用readLinesgrep/grepl 来帮助您确定要阅读哪些行。

这是一个例子。第一部分只是补充一些样本数据。

创建一些示例数据。

x <- tempfile(pattern="myFile", fileext=".csv")

cat("junk comment strings",
    "",
    "another junk comment string",
    "This,Is,My,Data",
    "1,2,3,4",
    "5,6,7,8",
    "",
    "back to comments",
    "This,Is,My,Data",
    "12,13,14,15",
    "15,16,17,18",
    "19,20,21,22", file = x, sep = "\n")

第一步:使用readLines()将数据导入R

在这一步中,我们还将删除我们不感兴趣的行。逻辑是我们只对具有以下形式的信息的行感兴趣(对于四列数据集):

某事逗号某事逗号某事逗号某事


## Read the data into R
## Replace "con" with the actual path to your file
A <- readLines(con = x)

## Find and extract the lines where there are "data".
## My example dataset only has 4 columns.
## Modify for your actual dataset.
A <- A[grepl(paste(rep(".*", 4), collapse=","), A)]

第 2 步:识别数据范围

## Identify the header rows. -1 for use with read.csv
HeaderRows <- grep("^This,Is", A)-1

## Identify the number of rows per data group
N <- c(diff(HeaderRows)-1, length(A)-1)

第三步:读取数据

使用数据范围信息指定读取前跳过多少行,读取多少行。

myData <- lapply(seq_along(HeaderRows), 
       function(x) read.csv(text = A, header = TRUE, 
                            nrows = N[x], skip = HeaderRows[x]))
myData
# [[1]]
#   This Is My Data
# 1    1  2  3    4
# 2    5  6  7    8
# 
# [[2]]
#   This Is My Data
# 1   12 13 14   15
# 2   15 16 17   18
# 3   19 20 21   22

如果您希望将所有这些都放在一个 data.frame 而不是 list 中,请使用:

final <- do.call(rbind, myData)

【讨论】:

  • OP 在哪里说文件中有多个标题行?如果不做出这种假设,解决方案可能会非常简单。你不觉得吗?
  • @asb,我假设来自this comment。当然,是的,如果不做这个假设,解决方案可以变得更简单。
  • 每个连续块的标题都相同
  • @jessica,那么这个答案应该有效,除非您的数据与您描述的场景不同。
  • 非常感谢您的帮助阿难。当您使用 x
【解决方案2】:

我最近遇到了这样的问题。我的解决方案是使用 awk 分离出不同类型的行,将它们加载到 dbms 中的不同表中,并使用 sql 创建一个平面文件以加载到 R 中。

或者,如果您不关心注释字符串,也许您可​​以只提取您想要的数据并加载它。

【讨论】:

  • 我不知道 awk。在这一点上将很难学习。将数据拉入R时,有没有办法指定excel数据范围?那么告诉 R,例如从 A1:Q352 中提取数据?
  • 对于你需要做的事情并不难,但它不是像 unix 那样的 windows 标准工具。如果您想要的所有数据都在 excel 的一个范围内,为什么不直接编辑 excel 文件并另存为 csv?如果您需要更多帮助,请发布一小部分真实数据。
【解决方案3】:

使用@Ananda Mahto 生成的数据,

file = x # change for the actual file name
alldata = readLines(file) # read all data
# count the fields in data (separated by comma)
nfields = count.fields(file=textConnection(alldata), sep=",", blank.lines.skip=FALSE) 
# asumme data has the 'mode' of the number of fields (can change for the actual number of colums)
dataFields = as.numeric(names(table(nfields))[which.max(table(nfields))]) 

alldata = alldata[nfields == dataFields] # read data lines only
header = alldata[1] # the header
alldata = c(header, alldata[alldata!=header]) # remove the extra headers
datos = read.csv(text=alldata) # read the data

  This Is My Data
1    1  2  3    4
2    5  6  7    8
3   12 13 14   15
4   15 16 17   18
5   19 20 21   22

【讨论】:

  • 你可以缩短一点:A &lt;- readLines(con = x); nFields &lt;- count.fields(file = textConnection(A), sep=",", blank.lines.skip = FALSE); A &lt;- A[nFields == max(nFields)]; read.csv(text = A[-grep("^This,Is", A)[-1]])
  • 我想要字段数的“模式”,而不是最大值(可能在 cmets 中有很多逗号)。此外,我们不知道标头是什么,但所有数据块都有相同的标头。我正在使用它。
  • @AnandaMahto,我根据你的建议只读取了一次数据,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 2017-07-27
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
相关资源
最近更新 更多