Replacing an element within a vector in a list
up vote
1
down vote
favorite
I have a list of vectors that looks like
[[1]][1] 1 1 2
[[2]]
[1] 1 1 2
[[3]]
[1] 2 1 1
[[4]]
[1] 2 2 2
I would like the replace the first component of each of the vectors with a 9
. I have tried
out <- append(vecs2T2[[1]], y, after=0)
but this just adds an 9
in at the start and does not replace it (see below).
[1] 9 1 1 2
I would like this entry to read 912
.
r
New contributor
add a comment |
up vote
1
down vote
favorite
I have a list of vectors that looks like
[[1]][1] 1 1 2
[[2]]
[1] 1 1 2
[[3]]
[1] 2 1 1
[[4]]
[1] 2 2 2
I would like the replace the first component of each of the vectors with a 9
. I have tried
out <- append(vecs2T2[[1]], y, after=0)
but this just adds an 9
in at the start and does not replace it (see below).
[1] 9 1 1 2
I would like this entry to read 912
.
r
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a list of vectors that looks like
[[1]][1] 1 1 2
[[2]]
[1] 1 1 2
[[3]]
[1] 2 1 1
[[4]]
[1] 2 2 2
I would like the replace the first component of each of the vectors with a 9
. I have tried
out <- append(vecs2T2[[1]], y, after=0)
but this just adds an 9
in at the start and does not replace it (see below).
[1] 9 1 1 2
I would like this entry to read 912
.
r
New contributor
I have a list of vectors that looks like
[[1]][1] 1 1 2
[[2]]
[1] 1 1 2
[[3]]
[1] 2 1 1
[[4]]
[1] 2 2 2
I would like the replace the first component of each of the vectors with a 9
. I have tried
out <- append(vecs2T2[[1]], y, after=0)
but this just adds an 9
in at the start and does not replace it (see below).
[1] 9 1 1 2
I would like this entry to read 912
.
r
r
New contributor
New contributor
edited Nov 21 at 14:35
Sotos
27k51540
27k51540
New contributor
asked Nov 21 at 14:23
Sarah
93
93
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
lapply(ll, replace, 1, 9)
This goes vector by vector, and replace
s the 1st item with 9. (Replace's arguments are: (data, list-of-indexes, list-of-values), with the list of values recycled to be as long as the list of indexes.)
replace()
is just defined as:
replace <- function (x, list, values) {
x[list] <- values
x
}
so you can also use that method.
lapply(ll, function(x) { x[1] <- 9 ; x })
You can use either with purrr::map()
, too:
purrr::map(ll, ~{ .x[1] <- 9 ; .x })
purrr::map(ll, replace, 1, 9)
Head-to-head (not the best microbenchmark setup in the world tho):
microbenchmark::microbenchmark(
purr_repl = purrr::map(ll, replace, 1, 9),
purr_op = purrr::map(ll, ~{ .x[1] <- 9 ; .x }),
lapp_repl = lapply(ll, replace, 1, 9),
lapp_op = lapply(ll, function(x) { x[1] <- 9 ; x }),
Map = Map(function(x, y)c(x, y[-1]), 9, ll)
)
## Unit: microseconds
## expr min lq mean median uq max neval
## purr_repl 27.510 29.7555 49.98242 31.4735 33.4805 1506.400 100
## purr_op 84.415 86.9550 125.07364 90.0665 98.9465 2423.406 100
## lapp_repl 4.422 4.8350 5.94472 5.1965 5.5930 34.947 100
## lapp_op 4.672 5.4250 19.14590 5.9045 6.5015 1215.477 100
## Map 10.670 12.2490 28.94712 13.5935 14.7170 1238.311 100
That worked perfectly. Thank you
– Sarah
Nov 21 at 14:31
Happy to hear. Don't forget to accept! :)
– iod
Nov 21 at 14:33
@hrbrmstr - thanks for all the edits, but the bottom line from your comparison at the bottom is that my original solution is the fastest one, by a lot, isn't it?
– iod
Nov 21 at 14:42
Yep, hence why I added it. Your solution was 👍🏼 (some folks have their heads stuck in the tidyverse so they'll appreciate the purrr additions despite the serious performance hit). Adding the alternates outside your answer seemed wrong since it's all the same thing (just less performant)
– hrbrmstr
Nov 21 at 14:45
add a comment |
up vote
0
down vote
Another idea is to use Map
and concatenate 9
with the each vector minus its first element
Map(function(x, y)c(x, y[-1]), 9, l1)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
lapply(ll, replace, 1, 9)
This goes vector by vector, and replace
s the 1st item with 9. (Replace's arguments are: (data, list-of-indexes, list-of-values), with the list of values recycled to be as long as the list of indexes.)
replace()
is just defined as:
replace <- function (x, list, values) {
x[list] <- values
x
}
so you can also use that method.
lapply(ll, function(x) { x[1] <- 9 ; x })
You can use either with purrr::map()
, too:
purrr::map(ll, ~{ .x[1] <- 9 ; .x })
purrr::map(ll, replace, 1, 9)
Head-to-head (not the best microbenchmark setup in the world tho):
microbenchmark::microbenchmark(
purr_repl = purrr::map(ll, replace, 1, 9),
purr_op = purrr::map(ll, ~{ .x[1] <- 9 ; .x }),
lapp_repl = lapply(ll, replace, 1, 9),
lapp_op = lapply(ll, function(x) { x[1] <- 9 ; x }),
Map = Map(function(x, y)c(x, y[-1]), 9, ll)
)
## Unit: microseconds
## expr min lq mean median uq max neval
## purr_repl 27.510 29.7555 49.98242 31.4735 33.4805 1506.400 100
## purr_op 84.415 86.9550 125.07364 90.0665 98.9465 2423.406 100
## lapp_repl 4.422 4.8350 5.94472 5.1965 5.5930 34.947 100
## lapp_op 4.672 5.4250 19.14590 5.9045 6.5015 1215.477 100
## Map 10.670 12.2490 28.94712 13.5935 14.7170 1238.311 100
That worked perfectly. Thank you
– Sarah
Nov 21 at 14:31
Happy to hear. Don't forget to accept! :)
– iod
Nov 21 at 14:33
@hrbrmstr - thanks for all the edits, but the bottom line from your comparison at the bottom is that my original solution is the fastest one, by a lot, isn't it?
– iod
Nov 21 at 14:42
Yep, hence why I added it. Your solution was 👍🏼 (some folks have their heads stuck in the tidyverse so they'll appreciate the purrr additions despite the serious performance hit). Adding the alternates outside your answer seemed wrong since it's all the same thing (just less performant)
– hrbrmstr
Nov 21 at 14:45
add a comment |
up vote
1
down vote
lapply(ll, replace, 1, 9)
This goes vector by vector, and replace
s the 1st item with 9. (Replace's arguments are: (data, list-of-indexes, list-of-values), with the list of values recycled to be as long as the list of indexes.)
replace()
is just defined as:
replace <- function (x, list, values) {
x[list] <- values
x
}
so you can also use that method.
lapply(ll, function(x) { x[1] <- 9 ; x })
You can use either with purrr::map()
, too:
purrr::map(ll, ~{ .x[1] <- 9 ; .x })
purrr::map(ll, replace, 1, 9)
Head-to-head (not the best microbenchmark setup in the world tho):
microbenchmark::microbenchmark(
purr_repl = purrr::map(ll, replace, 1, 9),
purr_op = purrr::map(ll, ~{ .x[1] <- 9 ; .x }),
lapp_repl = lapply(ll, replace, 1, 9),
lapp_op = lapply(ll, function(x) { x[1] <- 9 ; x }),
Map = Map(function(x, y)c(x, y[-1]), 9, ll)
)
## Unit: microseconds
## expr min lq mean median uq max neval
## purr_repl 27.510 29.7555 49.98242 31.4735 33.4805 1506.400 100
## purr_op 84.415 86.9550 125.07364 90.0665 98.9465 2423.406 100
## lapp_repl 4.422 4.8350 5.94472 5.1965 5.5930 34.947 100
## lapp_op 4.672 5.4250 19.14590 5.9045 6.5015 1215.477 100
## Map 10.670 12.2490 28.94712 13.5935 14.7170 1238.311 100
That worked perfectly. Thank you
– Sarah
Nov 21 at 14:31
Happy to hear. Don't forget to accept! :)
– iod
Nov 21 at 14:33
@hrbrmstr - thanks for all the edits, but the bottom line from your comparison at the bottom is that my original solution is the fastest one, by a lot, isn't it?
– iod
Nov 21 at 14:42
Yep, hence why I added it. Your solution was 👍🏼 (some folks have their heads stuck in the tidyverse so they'll appreciate the purrr additions despite the serious performance hit). Adding the alternates outside your answer seemed wrong since it's all the same thing (just less performant)
– hrbrmstr
Nov 21 at 14:45
add a comment |
up vote
1
down vote
up vote
1
down vote
lapply(ll, replace, 1, 9)
This goes vector by vector, and replace
s the 1st item with 9. (Replace's arguments are: (data, list-of-indexes, list-of-values), with the list of values recycled to be as long as the list of indexes.)
replace()
is just defined as:
replace <- function (x, list, values) {
x[list] <- values
x
}
so you can also use that method.
lapply(ll, function(x) { x[1] <- 9 ; x })
You can use either with purrr::map()
, too:
purrr::map(ll, ~{ .x[1] <- 9 ; .x })
purrr::map(ll, replace, 1, 9)
Head-to-head (not the best microbenchmark setup in the world tho):
microbenchmark::microbenchmark(
purr_repl = purrr::map(ll, replace, 1, 9),
purr_op = purrr::map(ll, ~{ .x[1] <- 9 ; .x }),
lapp_repl = lapply(ll, replace, 1, 9),
lapp_op = lapply(ll, function(x) { x[1] <- 9 ; x }),
Map = Map(function(x, y)c(x, y[-1]), 9, ll)
)
## Unit: microseconds
## expr min lq mean median uq max neval
## purr_repl 27.510 29.7555 49.98242 31.4735 33.4805 1506.400 100
## purr_op 84.415 86.9550 125.07364 90.0665 98.9465 2423.406 100
## lapp_repl 4.422 4.8350 5.94472 5.1965 5.5930 34.947 100
## lapp_op 4.672 5.4250 19.14590 5.9045 6.5015 1215.477 100
## Map 10.670 12.2490 28.94712 13.5935 14.7170 1238.311 100
lapply(ll, replace, 1, 9)
This goes vector by vector, and replace
s the 1st item with 9. (Replace's arguments are: (data, list-of-indexes, list-of-values), with the list of values recycled to be as long as the list of indexes.)
replace()
is just defined as:
replace <- function (x, list, values) {
x[list] <- values
x
}
so you can also use that method.
lapply(ll, function(x) { x[1] <- 9 ; x })
You can use either with purrr::map()
, too:
purrr::map(ll, ~{ .x[1] <- 9 ; .x })
purrr::map(ll, replace, 1, 9)
Head-to-head (not the best microbenchmark setup in the world tho):
microbenchmark::microbenchmark(
purr_repl = purrr::map(ll, replace, 1, 9),
purr_op = purrr::map(ll, ~{ .x[1] <- 9 ; .x }),
lapp_repl = lapply(ll, replace, 1, 9),
lapp_op = lapply(ll, function(x) { x[1] <- 9 ; x }),
Map = Map(function(x, y)c(x, y[-1]), 9, ll)
)
## Unit: microseconds
## expr min lq mean median uq max neval
## purr_repl 27.510 29.7555 49.98242 31.4735 33.4805 1506.400 100
## purr_op 84.415 86.9550 125.07364 90.0665 98.9465 2423.406 100
## lapp_repl 4.422 4.8350 5.94472 5.1965 5.5930 34.947 100
## lapp_op 4.672 5.4250 19.14590 5.9045 6.5015 1215.477 100
## Map 10.670 12.2490 28.94712 13.5935 14.7170 1238.311 100
edited Nov 21 at 14:35
hrbrmstr
58.9k584143
58.9k584143
answered Nov 21 at 14:29
iod
3,0271619
3,0271619
That worked perfectly. Thank you
– Sarah
Nov 21 at 14:31
Happy to hear. Don't forget to accept! :)
– iod
Nov 21 at 14:33
@hrbrmstr - thanks for all the edits, but the bottom line from your comparison at the bottom is that my original solution is the fastest one, by a lot, isn't it?
– iod
Nov 21 at 14:42
Yep, hence why I added it. Your solution was 👍🏼 (some folks have their heads stuck in the tidyverse so they'll appreciate the purrr additions despite the serious performance hit). Adding the alternates outside your answer seemed wrong since it's all the same thing (just less performant)
– hrbrmstr
Nov 21 at 14:45
add a comment |
That worked perfectly. Thank you
– Sarah
Nov 21 at 14:31
Happy to hear. Don't forget to accept! :)
– iod
Nov 21 at 14:33
@hrbrmstr - thanks for all the edits, but the bottom line from your comparison at the bottom is that my original solution is the fastest one, by a lot, isn't it?
– iod
Nov 21 at 14:42
Yep, hence why I added it. Your solution was 👍🏼 (some folks have their heads stuck in the tidyverse so they'll appreciate the purrr additions despite the serious performance hit). Adding the alternates outside your answer seemed wrong since it's all the same thing (just less performant)
– hrbrmstr
Nov 21 at 14:45
That worked perfectly. Thank you
– Sarah
Nov 21 at 14:31
That worked perfectly. Thank you
– Sarah
Nov 21 at 14:31
Happy to hear. Don't forget to accept! :)
– iod
Nov 21 at 14:33
Happy to hear. Don't forget to accept! :)
– iod
Nov 21 at 14:33
@hrbrmstr - thanks for all the edits, but the bottom line from your comparison at the bottom is that my original solution is the fastest one, by a lot, isn't it?
– iod
Nov 21 at 14:42
@hrbrmstr - thanks for all the edits, but the bottom line from your comparison at the bottom is that my original solution is the fastest one, by a lot, isn't it?
– iod
Nov 21 at 14:42
Yep, hence why I added it. Your solution was 👍🏼 (some folks have their heads stuck in the tidyverse so they'll appreciate the purrr additions despite the serious performance hit). Adding the alternates outside your answer seemed wrong since it's all the same thing (just less performant)
– hrbrmstr
Nov 21 at 14:45
Yep, hence why I added it. Your solution was 👍🏼 (some folks have their heads stuck in the tidyverse so they'll appreciate the purrr additions despite the serious performance hit). Adding the alternates outside your answer seemed wrong since it's all the same thing (just less performant)
– hrbrmstr
Nov 21 at 14:45
add a comment |
up vote
0
down vote
Another idea is to use Map
and concatenate 9
with the each vector minus its first element
Map(function(x, y)c(x, y[-1]), 9, l1)
add a comment |
up vote
0
down vote
Another idea is to use Map
and concatenate 9
with the each vector minus its first element
Map(function(x, y)c(x, y[-1]), 9, l1)
add a comment |
up vote
0
down vote
up vote
0
down vote
Another idea is to use Map
and concatenate 9
with the each vector minus its first element
Map(function(x, y)c(x, y[-1]), 9, l1)
Another idea is to use Map
and concatenate 9
with the each vector minus its first element
Map(function(x, y)c(x, y[-1]), 9, l1)
answered Nov 21 at 14:35
Sotos
27k51540
27k51540
add a comment |
add a comment |
Sarah is a new contributor. Be nice, and check out our Code of Conduct.
Sarah is a new contributor. Be nice, and check out our Code of Conduct.
Sarah is a new contributor. Be nice, and check out our Code of Conduct.
Sarah is a new contributor. Be nice, and check out our Code of Conduct.
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%2f53414188%2freplacing-an-element-within-a-vector-in-a-list%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