1) separate_rows Use separate_rows
to convert it to long form, add a name
column containing the eventual column names and use spread
to convert it back to wide form.
library(dplyr)
library(tidyr)
df %>%
separate_rows(stock) %>%
group_by(id) %>%
mutate(name = paste("stock", seq_along(stock), sep = "_")) %>%
ungroup %>%
spread(name, stock)
giving:
# A tibble: 3 x 3
id stock_1 stock_2
* <dbl> <chr> <chr>
1 1 Google Yahoo
2 2 Microsoft Google
3 3 Yahoo <NA>
2) separate If we knew that there were no more than 2 sub-fields then we could use separate
giving the same.
library(dplyr)
library(tidyr)
df %>%
separate(stock, c("stock_1", "stock_2"), fill = "right")
3) read.table This approach uses no packages.
stocks <- read.table(text = as.character(df$stock), sep = ";", as.is = TRUE, fill = TRUE)
names(stocks) <- paste("stock", seq_along(stocks), sep = "_")
cbind(df[1], stocks)
giving:
id stock_1 stock_2
1 1 Google Yahoo
2 2 Microsoft Google
3 3 Yahoo