如何从R中的.txt文件中提取特定行并将其存储在向量中

时间:2016-06-07 04:01:59

标签: r grep

我正在处理一个节目的成绩单,并希望提取每个演讲者的文本并将其存储到自己的矢量中。数据看起来像这样 -

BOB: blah blah blah blah

TRUDY: blah blah

BOB: you get the idea however some of the text  is on a new line like 

this so I don't know how to extract it to the correct vector

TRUDY: blah blah blah

..等等。

我想我需要使用readLines和grep的组合,但是我不确定如何实现它。

2 个答案:

答案 0 :(得分:1)

有趣的问题。不确定这是你需要的输出,但它应该给你一个好主意。

> bob
[1] "blah blah blah blah"                                                                                                          
[2] "you get the idea however some of the text is on a new line like  this so I don't know how to extract it to the correct vector"
[3] "Durrh!!!"                                                                                                                     
> trudy
[1] "bleh bleh"      "bleh bleh bleh"

结果

index: function(req, res) {
    Customer.find().populate('projects').exec(function(err, customers) {
        customers.forEach(function(customer, index) {
            customer.projects.forEach(function(project, index) {
                // Find project contributors and attach to project
                ProjectContributor.find({
                    project: project.id
                }).populate('user').exec(function(err, contributor) {
                    project.contributor = contributor;

                    return res.json(customers);
                });
            });
        });
    });
}

答案 1 :(得分:0)

假设text包含您的数据。然后试试这个

text <- readlines("data.txt")
pos <- which(stringr::word(text,1,1) %in% c("BOB:","TRUDY:"))
mapply(function(x,y){do.call(paste,as.list(text[x:y-1]))},pos,c(pos[-1],length(text)+1))