R - 原始文本到data.frame

时间:2015-11-21 10:37:39

标签: regex r string dataframe

我处理来自扫描目录的原始文本数据。 我想将我的字符串向量转换为data.frame对象。 我的向量包含按字母顺序排列的执行每项或多项工作的人员列表 - 人名是大写的 - 每件作品都有编号 - 编号工作是连续的。

AADFDS
1 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
AB 
2 Nulla sollicitudin elit in purus egestas, in placerat velit iaculis.
BBDDED
3 Nunc et eros eget turpis sollicitudin mollis id et mi.
4 Mauris condimentum velit eu consequat feugiat.
5 Suspendisse sit amet metus vitae est eleifend tincidunt.
CCDDFSF
6 Sed cursus augue in tempus scelerisque.
7 in commodo enim in laoreet gravida.

预期结果1

Author     Work  
AA DFDS    1 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
AB         2 Nulla sollicitudin elit in purus egestas, in placerat velit 
BBDDED     3 Nunc et eros eget turpis sollicitudin mollis id et 
BBDDED     4 Mauris condimentum velit eu consequat feugiat.
BBDDED     5 Suspendisse sit amet metus vitae est eleifend tincidunt.
CCDDFSF    6 Sed cursus augue in tempus scelerisque.
CCDDFSF    7 in commodo enim in laoreet gravida.

预期结果2,每个作品都有一列

Author  |   Work1  |  Work2  |  Work3  |  Work(x)  

将数据导入R:

readlines ("clipboard", encoding = " latin1 ")

我能够识别包含不同正则表达式的大写字母的艺术家姓名的行

e.g。

^[A-ZÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝYÆO][A-ZÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜÝYÆO |']

我能识别包括艺术品在内的一行

^[0-9]+[\s]

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

这样可以为您的样本数据提供正确的结果。

txt="
AADFDS
1 Lorem ipsum dolor sit amet, consectetur adipiscing elit.
AB 
2 Nulla sollicitudin elit in purus egestas, in placerat velit iaculis.
BBDDED
3 Nunc et eros eget turpis sollicitudin mollis id et mi.
4 Mauris condimentum velit eu consequat feugiat.
5 Suspendisse sit amet metus vitae est eleifend tincidunt.
CCDDFSF
6 Sed cursus augue in tempus scelerisque.
7 in commodo enim in laoreet gravida."

last_author=""
author_count=0
#the first scan splits the data by line, i.e., sep="\n"
#then for each line, we split by whitespace, i.e., sep=" "
#if the first element is numeric we increase the
#respective author's work counter "author_count" and
#we return the the work in a data.frame
#if the first element is non-numeric, we have
#encountered a new author
#we store the new author name in "last_author"
#(and remove trailing whitespaces at the end)
result1=do.call("rbind",
                lapply(as.list(scan(text=txt,
                                    what="character",
                                    sep="\n",
                                    quiet=TRUE)),
                       function(x) {
                         tmp=scan(text=x,what="character",sep=" ",quiet=TRUE)
                         if (grepl("[0-9]",tmp[1])) {
                           author_count<<-author_count+1
                           data.frame(Author=last_author,N=author_count,Work=x)
                         } else {
                           last_author<<-gsub("\\s*$","",x)
                           author_count<<-0
                           NULL
                         }}))

#we pivot the data; rows correspond to authors, columns to works
result2=reshape2::dcast(result1,Author~N,value.var = "Work")
#just renaming the columns
colnames(result2)[-1]=paste0("Work",1:(ncol(result2)-1))
result2

答案 1 :(得分:2)

toydata<- readLines("clipboard")

#find lines beginning with any number; flags lines with authors
work_id <- grepl("^[0-9]" , toydata)

#rle finds subsequent runs of an element within a vector
RLE <- rle(work_id)

#work_id filters out the lines with author names
#rep(toydata[!work_id],RLE$lengths[RLE$values]) repeats the ...
#... author name (times = number of author's works)
df_toydata <- data.frame(work = toydata[work_id],
                     Author = rep(toydata[!work_id],
                                  RLE$lengths[RLE$values]),
                     stringsAsFactors=FALSE)

#we have to order the data.frame by author just in case
#some author appears again
df_toydata=df_toydata[order(df_toydata$Author),]
#we can now add a column with a numbering of each author's works
df_toydata$N=sequence(rle(df_toydata$Author)$lengths)

#format long to large
#we pivot the data; rows correspond to authors, columns to works
df2=reshape2::dcast(df_toydata,Author~N,value.var = "work")
colnames(df2)[-1]=paste0("Work",1:(ncol(df2)-1))