longer object length is not a multiple of shorter object length

Published by onesixx on

https://stackoverflow.com/questions/16143700/pasting-two-vectors-with-combinations-of-all-vectors-elements

> str_c(c("a","b"), c("d","e","f"))
Warning in stri_c(..., sep = sep, collapse = collapse, ignore_null = TRUE) :
  longer object length is not a multiple of shorter object length

[1] "ad" "be" "af"

cross-product combination of two string arrays

fstS <- c("a","b")
sndS <- c("d","e","f")

str_c(fstS, sndS)
# 원하는 결과가 아님..
# [1] "ad" "be" "af"

# [1] "ad" "bd" "ae" "be" "af" "bf"
# stringr
str_c( rep(fstS, each=length(sndS)),  sndS)
# apply
expand.grid(fstS, sndS) %>% apply(1, str_c, collapse="")
do.call(str_c, expand.grid(fstS, sndS))
# data.table
CJ(fstS, sndS)[ , str_c(fstS, sndS)]
# base
outer(fstS, sndS, str_c) %>% as.vector()
# purrr
cross(list(fstS, sndS)) %>% map_chr(str_c, collapse="")
cross2(fstS, sndS) %>% map_chr(str_c, collapse="")

Categories: R Basic

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x