Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why do I get different outputs for calling "replicate" with and without pipe "%>%" in R? #273

Open
alireza5969 opened this issue Jun 30, 2024 · 0 comments

Comments

@alireza5969
Copy link

alireza5969 commented Jun 30, 2024

Hi!
This is more of a question than a bug or issue.

So I asked it on StackOverflow. But I ask it here in order to be found more easily by other users who encounter the same question.

The question

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] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJ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FyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[15] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[16] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[17] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[18] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[19] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"
[20] "yNLJofKIlodQuFVzseeYOmjcxJRxFzgSXWCFoBhCFyJplcgVrrIEhoSSDJXHWhlkWkDHdtKexZFEvIHFokUNg"

What do you think the problem is here?

ALSO
Other users have found confusing results, too:

Oh wow! Base R's new pipe, |> behaves differently: sample(x = c(letters, LETTERS), size = sample.int(100, 1), replace = T) |> replicate(n = 20, expr = _, simplify = "array"). At first, I agree with your results but base R's pipe now confuses me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant