0

I want to split column into multiple columns by matching the patterns

test <- data.frame("id" = c("Albertson's Inc.","Albertson's Inc."), "V3" = c("Reiterates FY 2004, Significant Developments, 2 June 2004, 53 words, (English)(Document MULTI00020050122e06201fkk)","EBITDA Hits Four Year Low,  Stock Diagnostics, 16:00 GMT, 9 June 2004, 245 words, (English)(Document STODIA0020040609e0690006g)"), stringsAsFactors = F)

So far the code I'm using to get desired result is like

library(stringr)
    df <- as.data.frame(str_match(test$V3, "^(.*)GMT,(.*),(.*)words,(.*)Document (.*)$")[,-1], stringsAsFactors = F)

I'm having two issues with above code First it does not show results when GMT is missing secondly I want "id" column in the output df as well, any suggestion or different approach should I use for results please share thanks to all the moderators programmers for such a helpful forum.

1 Answer 1

2

not 100% sure about your "GTM" problem. here is my try:

your rep data:

test <- data.frame("id" = c("Albertson's Inc.","Albertson's Inc."), "V3" = c("Reiterates FY 2004, Significant Developments, 2 June 2004, 53 words, (English)(Document MULTI00020050122e06201fkk)","EBITDA Hits Four Year Low,  Stock Diagnostics, 16:00 GMT, 9 June 2004, 245 words, (English)(Document STODIA0020040609e0690006g)"), stringsAsFactors = F)

code:

library(tidyverse)    
test$V3 %>% map(~str_split(.,",(?!\\s*\\d{1,2}:\\d{1,2})|(?<=\\))(?=\\()") %>% unlist %>% trimws) %>%
        do.call(rbind,.) %>%
        cbind(test["id"],.)

result:

#                    id                         1                            2           3         4         5                                    6
#    1 Albertson's Inc.        Reiterates FY 2004     Significant Developments 2 June 2004  53 words (English) (Document MULTI00020050122e06201fkk)
#    2 Albertson's Inc. EBITDA Hits Four Year Low Stock Diagnostics, 16:00 GMT 9 June 2004 245 words (English) (Document STODIA0020040609e0690006g)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.