Set all vector elements to NA in a list of vectors
How can I set all vector elements to NA in a list of vectors?
Essentially, I'd like to keep an existing list's structure and names but empty all values, to fill them in later. I provide a minimal example with a couple solutions below. I prefer base and tidyverse (esp. purrr) solutions, but can get on board with any approach which is better than what I have below.
my_list <- list(A = c('a' = 1, 'b' = 2, 'c' = 3), B = c('x' = 10, 'y' = 20))
ret_list <- my_list
# Approach 1
for (element_name in names(my_list)) {
ret_list[[element_name]] <- NA
}
ret_list
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
# Approach 2
lapply(my_list, function(x) {x <- NA; return(x)})
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
r
add a comment |
How can I set all vector elements to NA in a list of vectors?
Essentially, I'd like to keep an existing list's structure and names but empty all values, to fill them in later. I provide a minimal example with a couple solutions below. I prefer base and tidyverse (esp. purrr) solutions, but can get on board with any approach which is better than what I have below.
my_list <- list(A = c('a' = 1, 'b' = 2, 'c' = 3), B = c('x' = 10, 'y' = 20))
ret_list <- my_list
# Approach 1
for (element_name in names(my_list)) {
ret_list[[element_name]] <- NA
}
ret_list
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
# Approach 2
lapply(my_list, function(x) {x <- NA; return(x)})
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
r
1
both approaches seem fine to me (especially if you define the function in Approach 2 beforehand rather than anonymously inline). Why try to be cleverer than that? What specific shortcomings would you like to remedy?
– Ben Bolker
Dec 31 '18 at 23:27
1
Just taking an opportunity to understand R a little better. It seemed to me that this was such a common operation that there had to be a standard, best-practices approach.
– lowndrul
Dec 31 '18 at 23:40
add a comment |
How can I set all vector elements to NA in a list of vectors?
Essentially, I'd like to keep an existing list's structure and names but empty all values, to fill them in later. I provide a minimal example with a couple solutions below. I prefer base and tidyverse (esp. purrr) solutions, but can get on board with any approach which is better than what I have below.
my_list <- list(A = c('a' = 1, 'b' = 2, 'c' = 3), B = c('x' = 10, 'y' = 20))
ret_list <- my_list
# Approach 1
for (element_name in names(my_list)) {
ret_list[[element_name]] <- NA
}
ret_list
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
# Approach 2
lapply(my_list, function(x) {x <- NA; return(x)})
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
r
How can I set all vector elements to NA in a list of vectors?
Essentially, I'd like to keep an existing list's structure and names but empty all values, to fill them in later. I provide a minimal example with a couple solutions below. I prefer base and tidyverse (esp. purrr) solutions, but can get on board with any approach which is better than what I have below.
my_list <- list(A = c('a' = 1, 'b' = 2, 'c' = 3), B = c('x' = 10, 'y' = 20))
ret_list <- my_list
# Approach 1
for (element_name in names(my_list)) {
ret_list[[element_name]] <- NA
}
ret_list
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
# Approach 2
lapply(my_list, function(x) {x <- NA; return(x)})
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
r
r
edited Jan 1 at 0:02
lowndrul
asked Dec 31 '18 at 22:56
lowndrullowndrul
1,24032037
1,24032037
1
both approaches seem fine to me (especially if you define the function in Approach 2 beforehand rather than anonymously inline). Why try to be cleverer than that? What specific shortcomings would you like to remedy?
– Ben Bolker
Dec 31 '18 at 23:27
1
Just taking an opportunity to understand R a little better. It seemed to me that this was such a common operation that there had to be a standard, best-practices approach.
– lowndrul
Dec 31 '18 at 23:40
add a comment |
1
both approaches seem fine to me (especially if you define the function in Approach 2 beforehand rather than anonymously inline). Why try to be cleverer than that? What specific shortcomings would you like to remedy?
– Ben Bolker
Dec 31 '18 at 23:27
1
Just taking an opportunity to understand R a little better. It seemed to me that this was such a common operation that there had to be a standard, best-practices approach.
– lowndrul
Dec 31 '18 at 23:40
1
1
both approaches seem fine to me (especially if you define the function in Approach 2 beforehand rather than anonymously inline). Why try to be cleverer than that? What specific shortcomings would you like to remedy?
– Ben Bolker
Dec 31 '18 at 23:27
both approaches seem fine to me (especially if you define the function in Approach 2 beforehand rather than anonymously inline). Why try to be cleverer than that? What specific shortcomings would you like to remedy?
– Ben Bolker
Dec 31 '18 at 23:27
1
1
Just taking an opportunity to understand R a little better. It seemed to me that this was such a common operation that there had to be a standard, best-practices approach.
– lowndrul
Dec 31 '18 at 23:40
Just taking an opportunity to understand R a little better. It seemed to me that this was such a common operation that there had to be a standard, best-practices approach.
– lowndrul
Dec 31 '18 at 23:40
add a comment |
5 Answers
5
active
oldest
votes
Here's another one for numeric vectors:
lapply(my_list, `*`, NA) # Instead of * it could also be +, -, etc.
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
More generally,
lapply(my_list, replace, TRUE, NA)
and
lapply(ret_list, ifelse, NA, NA)
what is*
called as in this context ?
– YOLO
Dec 31 '18 at 23:12
1
@YOLO, I'm not sure what you mean. The first approach is the same aslapply(my_list, function(x) x * NA)
, so*
is the usual product, which results toNA
when one of the factors isNA
.
– Julius Vainora
Dec 31 '18 at 23:15
add a comment |
You can use function is.na<-
in a lapply
loop.
ret_list <- lapply(my_list, `is.na<-`)
ret_list
#$A
# a b c
#NA NA NA
#
#$B
# x y
#NA NA
add a comment |
another alternative with dplyr
:
lapply(my_list, function(x) dplyr::na_if(x,x))
add a comment |
If the list is not restricted to one level then use rapply
.
# test data modified from question
my_list2 <- list(list(A = c(a = 1, b = 2, c = 3)), B = c(x = 10, y = 20))
rapply(my_list2, function(x) replace(x, TRUE, NA), how = "list")
which can also be written as:
rapply(my_list2, replace, list = TRUE, values = NA, how = "list")
add a comment |
Another way around
relist(replace( unlist(my_list), TRUE, NA ), skeleton = my_list)
#$A
# a b c
#NA NA NA
#$B
# x y
#NA NA
add a comment |
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
});
}
});
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%2f53991996%2fset-all-vector-elements-to-na-in-a-list-of-vectors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here's another one for numeric vectors:
lapply(my_list, `*`, NA) # Instead of * it could also be +, -, etc.
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
More generally,
lapply(my_list, replace, TRUE, NA)
and
lapply(ret_list, ifelse, NA, NA)
what is*
called as in this context ?
– YOLO
Dec 31 '18 at 23:12
1
@YOLO, I'm not sure what you mean. The first approach is the same aslapply(my_list, function(x) x * NA)
, so*
is the usual product, which results toNA
when one of the factors isNA
.
– Julius Vainora
Dec 31 '18 at 23:15
add a comment |
Here's another one for numeric vectors:
lapply(my_list, `*`, NA) # Instead of * it could also be +, -, etc.
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
More generally,
lapply(my_list, replace, TRUE, NA)
and
lapply(ret_list, ifelse, NA, NA)
what is*
called as in this context ?
– YOLO
Dec 31 '18 at 23:12
1
@YOLO, I'm not sure what you mean. The first approach is the same aslapply(my_list, function(x) x * NA)
, so*
is the usual product, which results toNA
when one of the factors isNA
.
– Julius Vainora
Dec 31 '18 at 23:15
add a comment |
Here's another one for numeric vectors:
lapply(my_list, `*`, NA) # Instead of * it could also be +, -, etc.
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
More generally,
lapply(my_list, replace, TRUE, NA)
and
lapply(ret_list, ifelse, NA, NA)
Here's another one for numeric vectors:
lapply(my_list, `*`, NA) # Instead of * it could also be +, -, etc.
# $A
# a b c
# NA NA NA
#
# $B
# x y
# NA NA
More generally,
lapply(my_list, replace, TRUE, NA)
and
lapply(ret_list, ifelse, NA, NA)
edited Jan 1 at 14:28
answered Dec 31 '18 at 22:58
Julius VainoraJulius Vainora
38k76584
38k76584
what is*
called as in this context ?
– YOLO
Dec 31 '18 at 23:12
1
@YOLO, I'm not sure what you mean. The first approach is the same aslapply(my_list, function(x) x * NA)
, so*
is the usual product, which results toNA
when one of the factors isNA
.
– Julius Vainora
Dec 31 '18 at 23:15
add a comment |
what is*
called as in this context ?
– YOLO
Dec 31 '18 at 23:12
1
@YOLO, I'm not sure what you mean. The first approach is the same aslapply(my_list, function(x) x * NA)
, so*
is the usual product, which results toNA
when one of the factors isNA
.
– Julius Vainora
Dec 31 '18 at 23:15
what is
*
called as in this context ?– YOLO
Dec 31 '18 at 23:12
what is
*
called as in this context ?– YOLO
Dec 31 '18 at 23:12
1
1
@YOLO, I'm not sure what you mean. The first approach is the same as
lapply(my_list, function(x) x * NA)
, so *
is the usual product, which results to NA
when one of the factors is NA
.– Julius Vainora
Dec 31 '18 at 23:15
@YOLO, I'm not sure what you mean. The first approach is the same as
lapply(my_list, function(x) x * NA)
, so *
is the usual product, which results to NA
when one of the factors is NA
.– Julius Vainora
Dec 31 '18 at 23:15
add a comment |
You can use function is.na<-
in a lapply
loop.
ret_list <- lapply(my_list, `is.na<-`)
ret_list
#$A
# a b c
#NA NA NA
#
#$B
# x y
#NA NA
add a comment |
You can use function is.na<-
in a lapply
loop.
ret_list <- lapply(my_list, `is.na<-`)
ret_list
#$A
# a b c
#NA NA NA
#
#$B
# x y
#NA NA
add a comment |
You can use function is.na<-
in a lapply
loop.
ret_list <- lapply(my_list, `is.na<-`)
ret_list
#$A
# a b c
#NA NA NA
#
#$B
# x y
#NA NA
You can use function is.na<-
in a lapply
loop.
ret_list <- lapply(my_list, `is.na<-`)
ret_list
#$A
# a b c
#NA NA NA
#
#$B
# x y
#NA NA
answered Dec 31 '18 at 23:16
Rui BarradasRui Barradas
17.4k51731
17.4k51731
add a comment |
add a comment |
another alternative with dplyr
:
lapply(my_list, function(x) dplyr::na_if(x,x))
add a comment |
another alternative with dplyr
:
lapply(my_list, function(x) dplyr::na_if(x,x))
add a comment |
another alternative with dplyr
:
lapply(my_list, function(x) dplyr::na_if(x,x))
another alternative with dplyr
:
lapply(my_list, function(x) dplyr::na_if(x,x))
answered Dec 31 '18 at 23:36
Mankind_008Mankind_008
1,5582312
1,5582312
add a comment |
add a comment |
If the list is not restricted to one level then use rapply
.
# test data modified from question
my_list2 <- list(list(A = c(a = 1, b = 2, c = 3)), B = c(x = 10, y = 20))
rapply(my_list2, function(x) replace(x, TRUE, NA), how = "list")
which can also be written as:
rapply(my_list2, replace, list = TRUE, values = NA, how = "list")
add a comment |
If the list is not restricted to one level then use rapply
.
# test data modified from question
my_list2 <- list(list(A = c(a = 1, b = 2, c = 3)), B = c(x = 10, y = 20))
rapply(my_list2, function(x) replace(x, TRUE, NA), how = "list")
which can also be written as:
rapply(my_list2, replace, list = TRUE, values = NA, how = "list")
add a comment |
If the list is not restricted to one level then use rapply
.
# test data modified from question
my_list2 <- list(list(A = c(a = 1, b = 2, c = 3)), B = c(x = 10, y = 20))
rapply(my_list2, function(x) replace(x, TRUE, NA), how = "list")
which can also be written as:
rapply(my_list2, replace, list = TRUE, values = NA, how = "list")
If the list is not restricted to one level then use rapply
.
# test data modified from question
my_list2 <- list(list(A = c(a = 1, b = 2, c = 3)), B = c(x = 10, y = 20))
rapply(my_list2, function(x) replace(x, TRUE, NA), how = "list")
which can also be written as:
rapply(my_list2, replace, list = TRUE, values = NA, how = "list")
answered Jan 1 at 0:10
G. GrothendieckG. Grothendieck
150k10134238
150k10134238
add a comment |
add a comment |
Another way around
relist(replace( unlist(my_list), TRUE, NA ), skeleton = my_list)
#$A
# a b c
#NA NA NA
#$B
# x y
#NA NA
add a comment |
Another way around
relist(replace( unlist(my_list), TRUE, NA ), skeleton = my_list)
#$A
# a b c
#NA NA NA
#$B
# x y
#NA NA
add a comment |
Another way around
relist(replace( unlist(my_list), TRUE, NA ), skeleton = my_list)
#$A
# a b c
#NA NA NA
#$B
# x y
#NA NA
Another way around
relist(replace( unlist(my_list), TRUE, NA ), skeleton = my_list)
#$A
# a b c
#NA NA NA
#$B
# x y
#NA NA
answered Dec 31 '18 at 23:30
989989
9,02751834
9,02751834
add a comment |
add a comment |
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.
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%2f53991996%2fset-all-vector-elements-to-na-in-a-list-of-vectors%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
1
both approaches seem fine to me (especially if you define the function in Approach 2 beforehand rather than anonymously inline). Why try to be cleverer than that? What specific shortcomings would you like to remedy?
– Ben Bolker
Dec 31 '18 at 23:27
1
Just taking an opportunity to understand R a little better. It seemed to me that this was such a common operation that there had to be a standard, best-practices approach.
– lowndrul
Dec 31 '18 at 23:40