How do I repeat vector elements by times in another vector elements in R?
up vote
0
down vote
favorite
I want to repeat each
repeat.it <- c(0, 3951982, 7635488, 10986941)
by times:
repeat.times<- c(2L, 3L, 4L, 2L)
and get the result:
0,0,3951982,3951982,3951982,7635488,7635488,7635488,7635488,10986941,10986941
code I tried:
rep(repeat.it, each=repeat.times)
but this seems to give me wrong results. How can I do it properly?
r
add a comment |
up vote
0
down vote
favorite
I want to repeat each
repeat.it <- c(0, 3951982, 7635488, 10986941)
by times:
repeat.times<- c(2L, 3L, 4L, 2L)
and get the result:
0,0,3951982,3951982,3951982,7635488,7635488,7635488,7635488,10986941,10986941
code I tried:
rep(repeat.it, each=repeat.times)
but this seems to give me wrong results. How can I do it properly?
r
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to repeat each
repeat.it <- c(0, 3951982, 7635488, 10986941)
by times:
repeat.times<- c(2L, 3L, 4L, 2L)
and get the result:
0,0,3951982,3951982,3951982,7635488,7635488,7635488,7635488,10986941,10986941
code I tried:
rep(repeat.it, each=repeat.times)
but this seems to give me wrong results. How can I do it properly?
r
I want to repeat each
repeat.it <- c(0, 3951982, 7635488, 10986941)
by times:
repeat.times<- c(2L, 3L, 4L, 2L)
and get the result:
0,0,3951982,3951982,3951982,7635488,7635488,7635488,7635488,10986941,10986941
code I tried:
rep(repeat.it, each=repeat.times)
but this seems to give me wrong results. How can I do it properly?
r
r
edited Nov 18 at 15:04
asked Nov 18 at 14:56
MAPK
1,607829
1,607829
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
Almost there:
rep(repeat.it, times = repeat.times)
# [1] 0 0 3951982 3951982 3951982 7635488 7635488
# [8] 7635488 7635488 10986941 10986941
@MAPK, does it answer your question?
– Julius Vainora
Nov 23 at 18:22
add a comment |
up vote
1
down vote
This is an option
res <- mapply(rep, repeat.it, repeat.times)
res <- unlist(res)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Almost there:
rep(repeat.it, times = repeat.times)
# [1] 0 0 3951982 3951982 3951982 7635488 7635488
# [8] 7635488 7635488 10986941 10986941
@MAPK, does it answer your question?
– Julius Vainora
Nov 23 at 18:22
add a comment |
up vote
3
down vote
Almost there:
rep(repeat.it, times = repeat.times)
# [1] 0 0 3951982 3951982 3951982 7635488 7635488
# [8] 7635488 7635488 10986941 10986941
@MAPK, does it answer your question?
– Julius Vainora
Nov 23 at 18:22
add a comment |
up vote
3
down vote
up vote
3
down vote
Almost there:
rep(repeat.it, times = repeat.times)
# [1] 0 0 3951982 3951982 3951982 7635488 7635488
# [8] 7635488 7635488 10986941 10986941
Almost there:
rep(repeat.it, times = repeat.times)
# [1] 0 0 3951982 3951982 3951982 7635488 7635488
# [8] 7635488 7635488 10986941 10986941
answered Nov 18 at 15:02
Julius Vainora
27.4k75878
27.4k75878
@MAPK, does it answer your question?
– Julius Vainora
Nov 23 at 18:22
add a comment |
@MAPK, does it answer your question?
– Julius Vainora
Nov 23 at 18:22
@MAPK, does it answer your question?
– Julius Vainora
Nov 23 at 18:22
@MAPK, does it answer your question?
– Julius Vainora
Nov 23 at 18:22
add a comment |
up vote
1
down vote
This is an option
res <- mapply(rep, repeat.it, repeat.times)
res <- unlist(res)
add a comment |
up vote
1
down vote
This is an option
res <- mapply(rep, repeat.it, repeat.times)
res <- unlist(res)
add a comment |
up vote
1
down vote
up vote
1
down vote
This is an option
res <- mapply(rep, repeat.it, repeat.times)
res <- unlist(res)
This is an option
res <- mapply(rep, repeat.it, repeat.times)
res <- unlist(res)
answered Nov 18 at 15:02
JRR
7891219
7891219
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53362223%2fhow-do-i-repeat-vector-elements-by-times-in-another-vector-elements-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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