5

我有一个文本数据文件,我可能会使用readLines. 每个字符串的初始部分包含大量乱码,然后是我需要的数据。乱码和数据通常用三个点分隔。我想在最后三个点之后拆分字符串,或者用某种标记替换最后三个点,告诉 R 将这三个点左侧的所有内容视为一列。

这是 Stackoverflow 上的类似帖子,它将找到最后一个点:

R:查找字符串中的最后一个点

然而,在我的例子中,一些数据有小数,所以定位最后一个点是不够的。另外,我认为...在 R 中具有特殊含义,这可能会使问题复杂化。另一个潜在的并发症是一些点比其他点大。此外,在某些行中,三个点之一被替换为逗号。

除了gregexpr在上面的帖子中我尝试过使用gsub,但无法找出解决方案。

这是一个示例数据集和我希望达到的结果:

aa = matrix(c(
'first string of junk... 0.2 0 1', 
'next string ........2 0 2', 
'%%%... ! 1959 ...  0 3 3',
'year .. 2 .,.  7 6 5',
'this_string   is . not fine .•. 4 2 3'), 
nrow=5, byrow=TRUE,
dimnames = list(NULL, c("C1")))

aa <- as.data.frame(aa, stringsAsFactors=F)
aa

# desired result
#                             C1  C2 C3 C4
# 1        first string of junk  0.2  0  1
# 2            next string .....   2  0  2
# 3             %%%... ! 1959      0  3  3
# 4                 year .. 2      7  6  5
# 5 this_string   is . not fine    4  2  3

我希望这个问题不要被认为太具体。文本数据文件是使用我昨天关于在 R 中读取 MSWord 文件的帖子中概述的步骤创建的。

有些行不包含乱码或三个点,而只包含数据。但是,这可能是后续帖子的并发症。

谢谢你的任何建议。

4

3 回答 3

5

这可以解决问题,虽然不是特别优雅......

options(stringsAsFactors = FALSE)


# Search for three consecutive characters of your delimiters, then pull out
# all of the characters after that
# (in parentheses, represented in replace by \\1)
nums <- as.vector(gsub(aa$C1, pattern = "^.*[.,•]{3}\\s*(.*)", replace = "\\1"))

# Use strsplit to break the results apart at spaces and just get the numbers
# Use unlist to conver that into a bare vector of numbers
# Use matrix(, nrow = length(x)) to convert it back into a
# matrix of appropriate length
num.mat <- do.call(rbind, strsplit(nums, split = " "))


# Mash it back together with your original strings
result <- as.data.frame(cbind(aa, num.mat))

# Give it informative names
names(result) <- c("original.string", "num1", "num2", "num3")
于 2012-06-20T20:42:25.300 回答
2

这将使您大部分时间到达那里,并且包含逗号的数字不会有问题:

# First, use a regex to eliminate the bad pattern.  This regex
# eliminates any three-character combination of periods, commas,
# and big dots (•), so long as the combination is followed by 
# 0-2 spaces and then a digit.
aa.sub <- as.matrix(
  apply(aa, 1, function (x) 
    gsub('[•.,]{3}(\\s{0,2}\\d)', '\\1', x, perl = TRUE)))

# Second: it looks as though you want your data split into columns.
# So this regex splits on spaces that are (a) preceded by a letter, 
# digit, or space, and (b) followed by a digit.  The result is a 
# list, each element of which is a list containing the parts of 
# one of the strings in aa.
aa.list <- apply(aa.sub, 1, function (x) 
  strsplit(x, '(?<=[\\w\\d\\s])\\s(?=\\d)', perl = TRUE))  

# Remove the second element in aa.  There is no space before the 
# first data column in this string.  As a result, strsplit() split
# it into three columns, not 4.  That in turn throws off the code
# below.
aa.list <- aa.list[-2]

# Make the data frame.
aa.list <- lapply(aa.list, unlist)  # convert list of lists to list of vectors
aa.df   <- data.frame(aa.list)      
aa.df   <- data.frame(t(aa.df), row.names = NULL, stringsAsFactors = FALSE) 

剩下的唯一事情是修改正则表达式,strsplit()以便它可以处理aa. 或者也许最好手动处理这样的情况。

于 2012-06-20T20:53:42.347 回答
0

反转字符串
如有必要,反转您正在搜索的模式 - 这不是您的情况
反转结果

[俳句伪代码]

a = 'first string of junk... 0.2 0 1' // string to search
b = 'junk' // pattern to match 

ra = reverseString(a) // now equals '1 0 2.0 ...knuj fo gnirts tsrif'
rb = reverseString (b) // now equals 'knuj'

// run your regular expression search / replace - search in 'ra' for 'rb'
// put the result in rResult
// and then unreverse the result
// apologies for not knowing the syntax for 'R' regex

[/haiku-伪代码]

于 2012-06-20T19:52:02.003 回答