Merge two arrays in R alternating their elements












1















Suppose I have two arrays of the same length (e.g. 1000), array1 and array2, that look like



   array 1= 0.7828479 0.7782587 0.697296 0.7847533 0.7963145 0.7742886 0.6367928 ...
array 2= 0.5324373 -0.5765472 -0.5020422 -0.3265736 -0.09438474 0.1368133 ...


and I want to merge them into a new array that looks like:



   first 20 elements of array 1 (i.e. array1[1], array1[2],...,array1[20])
first 10 elements of array 2 (i.e. array2[1], array2[2],...,array2[10])
second 20 elements of array 1 (i.e. array1[21], array1[2],...,array1[40])
second 10 elements of array 2 (i.e. array2[11], array2[2],...,array2[20])


and so on, until all the elements of one of the arrays had been used up. Then array1 will always be used up first and the resulting vector will have 20 and 10 consecutive items from array 1 and 2 respectively each time.










share|improve this question




















  • 2





    Can you clarify this more? do you mean vector or (multidimensional) array? I am also not sure what you mean by "and so on"; do you mean 20 and 10 consecutive items from array 1 and 2 respectively each time? Then array 1 will always be used up first, right? (you mentioned they are equal length)

    – MartijnVanAttekum
    Nov 26 '18 at 11:47











  • @MartijnVanAttekum, thanks for the questions. I've edited my post

    – Mark
    Nov 26 '18 at 11:59
















1















Suppose I have two arrays of the same length (e.g. 1000), array1 and array2, that look like



   array 1= 0.7828479 0.7782587 0.697296 0.7847533 0.7963145 0.7742886 0.6367928 ...
array 2= 0.5324373 -0.5765472 -0.5020422 -0.3265736 -0.09438474 0.1368133 ...


and I want to merge them into a new array that looks like:



   first 20 elements of array 1 (i.e. array1[1], array1[2],...,array1[20])
first 10 elements of array 2 (i.e. array2[1], array2[2],...,array2[10])
second 20 elements of array 1 (i.e. array1[21], array1[2],...,array1[40])
second 10 elements of array 2 (i.e. array2[11], array2[2],...,array2[20])


and so on, until all the elements of one of the arrays had been used up. Then array1 will always be used up first and the resulting vector will have 20 and 10 consecutive items from array 1 and 2 respectively each time.










share|improve this question




















  • 2





    Can you clarify this more? do you mean vector or (multidimensional) array? I am also not sure what you mean by "and so on"; do you mean 20 and 10 consecutive items from array 1 and 2 respectively each time? Then array 1 will always be used up first, right? (you mentioned they are equal length)

    – MartijnVanAttekum
    Nov 26 '18 at 11:47











  • @MartijnVanAttekum, thanks for the questions. I've edited my post

    – Mark
    Nov 26 '18 at 11:59














1












1








1








Suppose I have two arrays of the same length (e.g. 1000), array1 and array2, that look like



   array 1= 0.7828479 0.7782587 0.697296 0.7847533 0.7963145 0.7742886 0.6367928 ...
array 2= 0.5324373 -0.5765472 -0.5020422 -0.3265736 -0.09438474 0.1368133 ...


and I want to merge them into a new array that looks like:



   first 20 elements of array 1 (i.e. array1[1], array1[2],...,array1[20])
first 10 elements of array 2 (i.e. array2[1], array2[2],...,array2[10])
second 20 elements of array 1 (i.e. array1[21], array1[2],...,array1[40])
second 10 elements of array 2 (i.e. array2[11], array2[2],...,array2[20])


and so on, until all the elements of one of the arrays had been used up. Then array1 will always be used up first and the resulting vector will have 20 and 10 consecutive items from array 1 and 2 respectively each time.










share|improve this question
















Suppose I have two arrays of the same length (e.g. 1000), array1 and array2, that look like



   array 1= 0.7828479 0.7782587 0.697296 0.7847533 0.7963145 0.7742886 0.6367928 ...
array 2= 0.5324373 -0.5765472 -0.5020422 -0.3265736 -0.09438474 0.1368133 ...


and I want to merge them into a new array that looks like:



   first 20 elements of array 1 (i.e. array1[1], array1[2],...,array1[20])
first 10 elements of array 2 (i.e. array2[1], array2[2],...,array2[10])
second 20 elements of array 1 (i.e. array1[21], array1[2],...,array1[40])
second 10 elements of array 2 (i.e. array2[11], array2[2],...,array2[20])


and so on, until all the elements of one of the arrays had been used up. Then array1 will always be used up first and the resulting vector will have 20 and 10 consecutive items from array 1 and 2 respectively each time.







arrays r merge






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 11:59







Mark

















asked Nov 26 '18 at 11:36









MarkMark

244215




244215








  • 2





    Can you clarify this more? do you mean vector or (multidimensional) array? I am also not sure what you mean by "and so on"; do you mean 20 and 10 consecutive items from array 1 and 2 respectively each time? Then array 1 will always be used up first, right? (you mentioned they are equal length)

    – MartijnVanAttekum
    Nov 26 '18 at 11:47











  • @MartijnVanAttekum, thanks for the questions. I've edited my post

    – Mark
    Nov 26 '18 at 11:59














  • 2





    Can you clarify this more? do you mean vector or (multidimensional) array? I am also not sure what you mean by "and so on"; do you mean 20 and 10 consecutive items from array 1 and 2 respectively each time? Then array 1 will always be used up first, right? (you mentioned they are equal length)

    – MartijnVanAttekum
    Nov 26 '18 at 11:47











  • @MartijnVanAttekum, thanks for the questions. I've edited my post

    – Mark
    Nov 26 '18 at 11:59








2




2





Can you clarify this more? do you mean vector or (multidimensional) array? I am also not sure what you mean by "and so on"; do you mean 20 and 10 consecutive items from array 1 and 2 respectively each time? Then array 1 will always be used up first, right? (you mentioned they are equal length)

– MartijnVanAttekum
Nov 26 '18 at 11:47





Can you clarify this more? do you mean vector or (multidimensional) array? I am also not sure what you mean by "and so on"; do you mean 20 and 10 consecutive items from array 1 and 2 respectively each time? Then array 1 will always be used up first, right? (you mentioned they are equal length)

– MartijnVanAttekum
Nov 26 '18 at 11:47













@MartijnVanAttekum, thanks for the questions. I've edited my post

– Mark
Nov 26 '18 at 11:59





@MartijnVanAttekum, thanks for the questions. I've edited my post

– Mark
Nov 26 '18 at 11:59












1 Answer
1






active

oldest

votes


















1














It seems that you use vectors then. Does this work? (I use some randomly initialized vectors). This assumes that the size of your vectors is a multiple of the number of elements you want to take each time.



vec1 <- rnorm(100)
vec2 <- rnorm(100)
step1 <- 20
step2 <- 10
as.vector(sapply(0 : (length(vec1) / step1 - 1), function(idx){
c(vec1[1 : step1 + (idx * step1)], vec2[1 : step2 + (idx * step2)])}))





share|improve this answer
























  • Thank you @MartijnVanAttekum

    – Mark
    Nov 26 '18 at 12:36












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480275%2fmerge-two-arrays-in-r-alternating-their-elements%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














It seems that you use vectors then. Does this work? (I use some randomly initialized vectors). This assumes that the size of your vectors is a multiple of the number of elements you want to take each time.



vec1 <- rnorm(100)
vec2 <- rnorm(100)
step1 <- 20
step2 <- 10
as.vector(sapply(0 : (length(vec1) / step1 - 1), function(idx){
c(vec1[1 : step1 + (idx * step1)], vec2[1 : step2 + (idx * step2)])}))





share|improve this answer
























  • Thank you @MartijnVanAttekum

    – Mark
    Nov 26 '18 at 12:36
















1














It seems that you use vectors then. Does this work? (I use some randomly initialized vectors). This assumes that the size of your vectors is a multiple of the number of elements you want to take each time.



vec1 <- rnorm(100)
vec2 <- rnorm(100)
step1 <- 20
step2 <- 10
as.vector(sapply(0 : (length(vec1) / step1 - 1), function(idx){
c(vec1[1 : step1 + (idx * step1)], vec2[1 : step2 + (idx * step2)])}))





share|improve this answer
























  • Thank you @MartijnVanAttekum

    – Mark
    Nov 26 '18 at 12:36














1












1








1







It seems that you use vectors then. Does this work? (I use some randomly initialized vectors). This assumes that the size of your vectors is a multiple of the number of elements you want to take each time.



vec1 <- rnorm(100)
vec2 <- rnorm(100)
step1 <- 20
step2 <- 10
as.vector(sapply(0 : (length(vec1) / step1 - 1), function(idx){
c(vec1[1 : step1 + (idx * step1)], vec2[1 : step2 + (idx * step2)])}))





share|improve this answer













It seems that you use vectors then. Does this work? (I use some randomly initialized vectors). This assumes that the size of your vectors is a multiple of the number of elements you want to take each time.



vec1 <- rnorm(100)
vec2 <- rnorm(100)
step1 <- 20
step2 <- 10
as.vector(sapply(0 : (length(vec1) / step1 - 1), function(idx){
c(vec1[1 : step1 + (idx * step1)], vec2[1 : step2 + (idx * step2)])}))






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 12:14









MartijnVanAttekumMartijnVanAttekum

531211




531211













  • Thank you @MartijnVanAttekum

    – Mark
    Nov 26 '18 at 12:36



















  • Thank you @MartijnVanAttekum

    – Mark
    Nov 26 '18 at 12:36

















Thank you @MartijnVanAttekum

– Mark
Nov 26 '18 at 12:36





Thank you @MartijnVanAttekum

– Mark
Nov 26 '18 at 12:36




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480275%2fmerge-two-arrays-in-r-alternating-their-elements%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin