python/R string concatenation in recursion -
the data looks this
a, b, yymm 1, 1, 1707 1, 2, 1707 1, 3, 1707 2, 3, 1706 2, 1, 1706 2, 2, 1706 2, 4, 1706 3, 3, 1705 3, 2, 1705 3, 1, 1704 3, 4, 1704
i output source , target concatenate a,b follows:
source, target (1,1), (1,2) (1,1), (1,3) (1,2), (1,3) (2,3), (2,1) (2,3), (2,2) (2,3), (2,4) (2,1), (2,2) (2,1), (2,4) (2,2), (2,4) (3,3), (3,2) (3,1), (3,4)
basically, calculate possible number of cases concatenation yymm view relations between 2 columns.
at first, thought concatenating them through range : max-i , i+1 max, despite values being integer, feel should use them string since order matters.
is there function available manipulate dataset want? appreciate suggestions.
here 1 option. can use combn
function , tidyverse
package.
library(tidyverse) dt2 <- dt %>% unite(value, a, b, sep = ",") %>% split(f = .$yymm) %>% map(function(x){ as_data_frame(t(combn(x$value, m = 2))) }) %>% bind_rows(.id = "yymm") %>% arrange(desc(yymm)) %>% select(source = v1, target = v2) dt2 # tibble: 11 x 2 source target <chr> <chr> 1 1,1 1,2 2 1,1 1,3 3 1,2 1,3 4 2,3 2,1 5 2,3 2,2 6 2,3 2,4 7 2,1 2,2 8 2,1 2,4 9 2,2 2,4 10 3,3 3,2 11 3,1 3,4
Comments
Post a Comment