Question
Why do I get different outputs for calling "replicate" with and without pipe "%>%" in R?
I want to generate a character vector with 20 elements, each has a random string.
So I generate a random string with the following code:
sample(x = c(letters, LETTERS), size = sample.int(100, 1), replace = T) %>% paste0(collapse = "")
and I generate the vector with this:
replicate(n = 20, expr = sample(x = c(letters, LETTERS), size = sample.int(100, 1), replace = T) %>% paste0(collapse = ""))
(I am basically putting the first code chunk in replicate
as an argument.)
and the output is the following (which is exactly what I want):
[1] "DyHpcnruLfKHOsvy"
[2] "lQqwOkKD"
[3] "XddKDCtOJqZHxAgHqreDwWSBQkDCBdwFclHMFhuCzXXwb"
[4] "bmuiUUdsnHJsxIEyeLClvbLGBbfEgXFVsScrWxiTcZxPNTTwAJGZDVgJzDKUG"
[5] "yBDZYKxPXXGFwmlPWNMQuUJsfRXsBoQhuVXnYfMNkHFpmAgSRafGBzkKu"
[6] "LLuNdUoayRwtLqRJKrnxERpMmlntghfkjUqPkxMMubUozsLbPOFESOqtAWKoojOrttVCQlIYkyGRglr"
[7] "KuydhJOVZNNDrrMLDeWda"
[8] "ItwNtPGIQDsqCRBoVUCkClgHzCUiYRAiHIQRqpGBpfzRXgmWFArRtmnWhtciPgLlqrVs"
[9] "BqqEjCpUHLzOlsmqiAOchAKysbtUCzce"
[10] "JJzdyoFqFnZOeLAABK"
[11] "bakCawEaOkMspowlFUsAMjAbMxNxguHAHLomiGtenMuENNuPElGwqdqNdVS"
[12] "OEtuDejCDVfDwGjKbjWSCsicrRmqGGpWyqMfaGGPNkJhJMbgUtkjbcwitLqVojCERLxTWaCNFRltxgiwdJAbUtoksW"
[13] "crVVVzIyWbAlfyFndgipAZZJLcMqtEtZtBpbisbyAUWsKTJLiwyNvyVPPuoxOkafEeLARYDEOqEoh"
[14] "QgAZkacEMBbGebUCToXFTLSqqlYhqpbdsPYvIrwJhfpgDcPiJlfiATEEDrYahyXgxLEVXvsbQ"
[15] "jHSYxhskNMxYnnbGQLQgTJKsuRXEeDpiPlonDABrXxivwepNNvZGrugSfHoMi"
[16] "CdCDpUjlUyLwiujvcLcxpNZjtxUMTMVYxnjCEQqbJQOXZJeTLHXQRbHaIsOIDmKeyNainhphvwEAHscCAhOjUsqQe"
[17] "XvoelRDEYrxMfffBjRzmFPrLRjayCLRFVpWxzjcIxkRZQiPutModt"
[18] "FNjvlFdyrRTVDWvnXVWjckCDFUkxnbUfkqYDNIPZVMOfjUejEKiuhhTXdi"
[19] "qsQDQtaVyoNVHtNNltPqLEuNGDxiscsOsXZfhaUNdBCoSwcouhhpwFhfCcqYPPFXrnjKqnlEknuKsaWVizaIacMiT"
[20] "ykGqCILONPhzEABAuNjtEjzXxeFLnybwZdVEbDdzDQoDKmIiLvZNhoEEEvYJS"
But here is the problem. When I try to pipe the first code chuck into replicate
using magrittr
's pipe (%>%
), I get an output with similar strings!
This is how I do it:
sample(x = c(letters, LETTERS), size = sample.int(100, 1), replace = T) %>% paste0(collapse = "") %>% replicate(n = 20, expr = ., simplify = "array")
also, I tried to wrap them into parentheses:
(sample(x = c(letters, LETTERS), size = sample.int(100, 1), replace = T) %>% paste0(collapse = "")) %>% replicate(n = 20, expr = ., simplify = "array")
But, in both cases, this is the undesirable output:
[1] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[2] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[3] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[4] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[5] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
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yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[19] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[20] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
What do you think the problem is here?