Create a test base:
b <- data_frame(x = 1:10,y = 1:10, string = rep("123-235-203", 10))
With the following code you can get what you wanted:
b %>%
separate(string, into = c('s1', 's2', 's3'), sep = '-') %>%
gather(string, valor, -x, -y) %>%
select(-string) %>%
arrange(x)
- The function
separate
of tidyr
transforms your string into three columns (s1, s2 and s3).
- The function
gather
of tidyr
multiplies the lines.
- The function
select
of dplyr
removes the new string
column that is no longer needed.
-
arrange
is just to be easier to understand.
The results are below:
# Source: local data frame [30 x 3]
#
# x y valor
# 1 1 1 123
# 2 1 1 235
# 3 1 1 203
# 4 2 2 123
# 5 2 2 235
# 6 2 2 203
# 7 3 3 123
# 8 3 3 235
# 9 3 3 203
# 10 4 4 123
# .. . . ...
In case the strings can have variable sizes, but there is a maximum of "-" 's you can do as follows:
b <- data_frame(x = 1:10,y = 1:10, string = rep(c("123-203", "123-203-555"), length.out = 10))
b %>%
separate(string, sep = '-', into = c("s1", "s2", "s3"), extra = "merge") %>%
gather(string, valor, -x, -y, na.rm = T) %>%
select(-string) %>%
arrange(x)
- I added the argument
extra = "merge"
to separate
so that it does not return an error.
- I added the argument
na.rm = T
so it does not create lines with NA
.