1

I have a data frame with a name column and a ticker column, the ticker column trim with ";" if a name has more than 1 ticker. A glimpse from this data.frame below:

df.info is the name of the dataframe

Rows: 525
Columns: 2
$ name.company <chr> "521 PARTICIPAÇOES S.A. - EM LIQUIDAÇÃO EXTRAJUDICIAL", "524 PARTICIPAÇOES SA", "AAJR SECURITIZADORA DE CRÉDITO...
$ tickers      <chr> NA, "QVQP3B", NA, "ADHM3", "TIET11;TIET3;TIET4", "AFLT3", NA, "ALEF3B", "RPAD3;RPAD5;RPAD6", NA, "ALSO3", "ALPA...

And I want a dataframe that has 2 columns, ticker and name.company but without the trim pattern ";".

e.g.: 
name   ticker
tiete  tiet11
tiete  tiet3
tiete  tiet4 

and so it goes.. I solved it using the by() function but I have no clue how to solve it using the tidyverse/purrr packages.

Solution without tidyverse

get.ticker.df <- function(df.in)
{
   # Gets ticker string and organizes it in another data_frame
   temp.split <- str_split(df.in$tickers, ';')[[1]]
   temp.df <- tibble(name.company = df.in$name.company,
                         ticker = temp.split)
}
   
 my.l <- by(data = df.info,
               INDICES = df.info$name.company,
               FUN = get.ticker.df)
    
    df.tickers <- bind_rows(my.l)

I don't know the equivalent of this by() function in tidyverse.

Edit - Added initial frame and the ideal result dataframe, to make it clear.

tibble_start <- tibble( name.company = c("AES TIETE", "AMBEV"),
                        ticker = c("TIET11;TIET3;TIET4", "ABEV3;ABEV4"))

tibble_ideal <- tibble( name.company = c( rep("AES TIETE", 3), rep("AMBEV",2)),
                        ticker = c("TIET11","TIET3","TIET4","ABEV3","ABEV4"))

Thanks in advance!

Community
  • 1
  • 1

1 Answers1

0

We can use separate_rows

library(dplyr)
library(tidyr)
df1 %>%
  separate_rows(tickers)
akrun
  • 874,273
  • 37
  • 540
  • 662