remove columns with NA values from list of matrices
I have a list of matrices, and need to remove columns containing NA values, within each matrix.
I tried to use lapply with !is.na to apply this to all the matrices in the entire list, but what it returns is a list of vectors with NAs excluded, whereas I still want a list of matrices (just without the columns containing NA).
> my_list
$mat1
V1 V2 V3
[1,] 1 5 NA
[1,] 2 6 NA
[1,] 3 7 NA
[1,] 4 8 NA
$mat2
V1 V2 V3
[1,] 1 NA 9
[1,] 2 NA 10
[1,] 3 NA 11
[1,] 4 NA 12
> lapply(my_list,function(x) x[!is.na(x)])
$mat1
[1] 1 2 3 4 5 6 7 8
$mat2
[1] 1 2 3 4 9 10 11 12
The output I'm trying to get is this:
$mat1
V1 V2
[1,] 1 5
[1,] 2 6
[1,] 3 7
[1,] 4 8
$mat2
V1 V3
[1,] 1 9
[1,] 2 10
[1,] 3 11
[1,] 4 12
r matrix na
|
show 2 more comments
I have a list of matrices, and need to remove columns containing NA values, within each matrix.
I tried to use lapply with !is.na to apply this to all the matrices in the entire list, but what it returns is a list of vectors with NAs excluded, whereas I still want a list of matrices (just without the columns containing NA).
> my_list
$mat1
V1 V2 V3
[1,] 1 5 NA
[1,] 2 6 NA
[1,] 3 7 NA
[1,] 4 8 NA
$mat2
V1 V2 V3
[1,] 1 NA 9
[1,] 2 NA 10
[1,] 3 NA 11
[1,] 4 NA 12
> lapply(my_list,function(x) x[!is.na(x)])
$mat1
[1] 1 2 3 4 5 6 7 8
$mat2
[1] 1 2 3 4 9 10 11 12
The output I'm trying to get is this:
$mat1
V1 V2
[1,] 1 5
[1,] 2 6
[1,] 3 7
[1,] 4 8
$mat2
V1 V3
[1,] 1 9
[1,] 2 10
[1,] 3 11
[1,] 4 12
r matrix na
4
Please give a Minimal, Complete, and Verifiable example in your question!
– jogo
Nov 21 '18 at 9:27
1
lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])
– Andre Elrico
Nov 21 '18 at 9:28
1
@RLave complete.cases looks row-wise forNA. So not practical here
– Andre Elrico
Nov 21 '18 at 9:38
3
For one matrixapply(is.na(A), 2, any)gives you the logical index for the columns with NA. To remove the columns do:A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do:lapply(L, function(A) A[, !apply(is.na(A), 2, any)])
– jogo
Nov 21 '18 at 10:02
1
Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
– Neuroguy
Nov 21 '18 at 10:11
|
show 2 more comments
I have a list of matrices, and need to remove columns containing NA values, within each matrix.
I tried to use lapply with !is.na to apply this to all the matrices in the entire list, but what it returns is a list of vectors with NAs excluded, whereas I still want a list of matrices (just without the columns containing NA).
> my_list
$mat1
V1 V2 V3
[1,] 1 5 NA
[1,] 2 6 NA
[1,] 3 7 NA
[1,] 4 8 NA
$mat2
V1 V2 V3
[1,] 1 NA 9
[1,] 2 NA 10
[1,] 3 NA 11
[1,] 4 NA 12
> lapply(my_list,function(x) x[!is.na(x)])
$mat1
[1] 1 2 3 4 5 6 7 8
$mat2
[1] 1 2 3 4 9 10 11 12
The output I'm trying to get is this:
$mat1
V1 V2
[1,] 1 5
[1,] 2 6
[1,] 3 7
[1,] 4 8
$mat2
V1 V3
[1,] 1 9
[1,] 2 10
[1,] 3 11
[1,] 4 12
r matrix na
I have a list of matrices, and need to remove columns containing NA values, within each matrix.
I tried to use lapply with !is.na to apply this to all the matrices in the entire list, but what it returns is a list of vectors with NAs excluded, whereas I still want a list of matrices (just without the columns containing NA).
> my_list
$mat1
V1 V2 V3
[1,] 1 5 NA
[1,] 2 6 NA
[1,] 3 7 NA
[1,] 4 8 NA
$mat2
V1 V2 V3
[1,] 1 NA 9
[1,] 2 NA 10
[1,] 3 NA 11
[1,] 4 NA 12
> lapply(my_list,function(x) x[!is.na(x)])
$mat1
[1] 1 2 3 4 5 6 7 8
$mat2
[1] 1 2 3 4 9 10 11 12
The output I'm trying to get is this:
$mat1
V1 V2
[1,] 1 5
[1,] 2 6
[1,] 3 7
[1,] 4 8
$mat2
V1 V3
[1,] 1 9
[1,] 2 10
[1,] 3 11
[1,] 4 12
r matrix na
r matrix na
edited Nov 21 '18 at 10:07
Neuroguy
asked Nov 21 '18 at 9:26
NeuroguyNeuroguy
447
447
4
Please give a Minimal, Complete, and Verifiable example in your question!
– jogo
Nov 21 '18 at 9:27
1
lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])
– Andre Elrico
Nov 21 '18 at 9:28
1
@RLave complete.cases looks row-wise forNA. So not practical here
– Andre Elrico
Nov 21 '18 at 9:38
3
For one matrixapply(is.na(A), 2, any)gives you the logical index for the columns with NA. To remove the columns do:A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do:lapply(L, function(A) A[, !apply(is.na(A), 2, any)])
– jogo
Nov 21 '18 at 10:02
1
Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
– Neuroguy
Nov 21 '18 at 10:11
|
show 2 more comments
4
Please give a Minimal, Complete, and Verifiable example in your question!
– jogo
Nov 21 '18 at 9:27
1
lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])
– Andre Elrico
Nov 21 '18 at 9:28
1
@RLave complete.cases looks row-wise forNA. So not practical here
– Andre Elrico
Nov 21 '18 at 9:38
3
For one matrixapply(is.na(A), 2, any)gives you the logical index for the columns with NA. To remove the columns do:A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do:lapply(L, function(A) A[, !apply(is.na(A), 2, any)])
– jogo
Nov 21 '18 at 10:02
1
Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
– Neuroguy
Nov 21 '18 at 10:11
4
4
Please give a Minimal, Complete, and Verifiable example in your question!
– jogo
Nov 21 '18 at 9:27
Please give a Minimal, Complete, and Verifiable example in your question!
– jogo
Nov 21 '18 at 9:27
1
1
lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])– Andre Elrico
Nov 21 '18 at 9:28
lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])– Andre Elrico
Nov 21 '18 at 9:28
1
1
@RLave complete.cases looks row-wise for
NA. So not practical here– Andre Elrico
Nov 21 '18 at 9:38
@RLave complete.cases looks row-wise for
NA. So not practical here– Andre Elrico
Nov 21 '18 at 9:38
3
3
For one matrix
apply(is.na(A), 2, any) gives you the logical index for the columns with NA. To remove the columns do: A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do: lapply(L, function(A) A[, !apply(is.na(A), 2, any)])– jogo
Nov 21 '18 at 10:02
For one matrix
apply(is.na(A), 2, any) gives you the logical index for the columns with NA. To remove the columns do: A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do: lapply(L, function(A) A[, !apply(is.na(A), 2, any)])– jogo
Nov 21 '18 at 10:02
1
1
Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
– Neuroguy
Nov 21 '18 at 10:11
Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
– Neuroguy
Nov 21 '18 at 10:11
|
show 2 more comments
2 Answers
2
active
oldest
votes
For one matrix apply(is.na(mat), 2, any) gives you the logical index for the columns with NA. To remove the columns do:
mat[, !apply(is.na(mat), 2, any)]
To do this with a list of matrices do:
lapply(my_list, function(mat) mat[, !apply(is.na(mat), 2, any)])
Data:
> dput(my_list)
list(structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, NA, NA, NA,
NA), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2", "V3"
))), structure(c(1L, 2L, 3L, 4L, NA, NA, NA, NA, 9L, 10L, 11L,
12L), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2",
"V3"))))
add a comment |
Or another option is tidyverse with select_if after converting to data.frame
library(tidyverse)
map(lst1, ~ .x %>%
as.data.frame %>%
select_if(~ all(!is.na(.))))
#[[1]]
# V1 V2
#1 1 5
#2 2 6
#3 3 7
#4 4 8
#[[2]]
# V1 V3
#1 1 9
#2 2 10
#3 3 11
#4 4 12
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%2f53408869%2fremove-columns-with-na-values-from-list-of-matrices%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
For one matrix apply(is.na(mat), 2, any) gives you the logical index for the columns with NA. To remove the columns do:
mat[, !apply(is.na(mat), 2, any)]
To do this with a list of matrices do:
lapply(my_list, function(mat) mat[, !apply(is.na(mat), 2, any)])
Data:
> dput(my_list)
list(structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, NA, NA, NA,
NA), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2", "V3"
))), structure(c(1L, 2L, 3L, 4L, NA, NA, NA, NA, 9L, 10L, 11L,
12L), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2",
"V3"))))
add a comment |
For one matrix apply(is.na(mat), 2, any) gives you the logical index for the columns with NA. To remove the columns do:
mat[, !apply(is.na(mat), 2, any)]
To do this with a list of matrices do:
lapply(my_list, function(mat) mat[, !apply(is.na(mat), 2, any)])
Data:
> dput(my_list)
list(structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, NA, NA, NA,
NA), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2", "V3"
))), structure(c(1L, 2L, 3L, 4L, NA, NA, NA, NA, 9L, 10L, 11L,
12L), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2",
"V3"))))
add a comment |
For one matrix apply(is.na(mat), 2, any) gives you the logical index for the columns with NA. To remove the columns do:
mat[, !apply(is.na(mat), 2, any)]
To do this with a list of matrices do:
lapply(my_list, function(mat) mat[, !apply(is.na(mat), 2, any)])
Data:
> dput(my_list)
list(structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, NA, NA, NA,
NA), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2", "V3"
))), structure(c(1L, 2L, 3L, 4L, NA, NA, NA, NA, 9L, 10L, 11L,
12L), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2",
"V3"))))
For one matrix apply(is.na(mat), 2, any) gives you the logical index for the columns with NA. To remove the columns do:
mat[, !apply(is.na(mat), 2, any)]
To do this with a list of matrices do:
lapply(my_list, function(mat) mat[, !apply(is.na(mat), 2, any)])
Data:
> dput(my_list)
list(structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, NA, NA, NA,
NA), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2", "V3"
))), structure(c(1L, 2L, 3L, 4L, NA, NA, NA, NA, 9L, 10L, 11L,
12L), .Dim = c(4L, 3L), .Dimnames = list(NULL, c("V1", "V2",
"V3"))))
edited Nov 21 '18 at 10:39
answered Nov 21 '18 at 10:29
jogojogo
9,85692135
9,85692135
add a comment |
add a comment |
Or another option is tidyverse with select_if after converting to data.frame
library(tidyverse)
map(lst1, ~ .x %>%
as.data.frame %>%
select_if(~ all(!is.na(.))))
#[[1]]
# V1 V2
#1 1 5
#2 2 6
#3 3 7
#4 4 8
#[[2]]
# V1 V3
#1 1 9
#2 2 10
#3 3 11
#4 4 12
add a comment |
Or another option is tidyverse with select_if after converting to data.frame
library(tidyverse)
map(lst1, ~ .x %>%
as.data.frame %>%
select_if(~ all(!is.na(.))))
#[[1]]
# V1 V2
#1 1 5
#2 2 6
#3 3 7
#4 4 8
#[[2]]
# V1 V3
#1 1 9
#2 2 10
#3 3 11
#4 4 12
add a comment |
Or another option is tidyverse with select_if after converting to data.frame
library(tidyverse)
map(lst1, ~ .x %>%
as.data.frame %>%
select_if(~ all(!is.na(.))))
#[[1]]
# V1 V2
#1 1 5
#2 2 6
#3 3 7
#4 4 8
#[[2]]
# V1 V3
#1 1 9
#2 2 10
#3 3 11
#4 4 12
Or another option is tidyverse with select_if after converting to data.frame
library(tidyverse)
map(lst1, ~ .x %>%
as.data.frame %>%
select_if(~ all(!is.na(.))))
#[[1]]
# V1 V2
#1 1 5
#2 2 6
#3 3 7
#4 4 8
#[[2]]
# V1 V3
#1 1 9
#2 2 10
#3 3 11
#4 4 12
answered Nov 21 '18 at 18:03
akrunakrun
400k13189264
400k13189264
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%2f53408869%2fremove-columns-with-na-values-from-list-of-matrices%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
4
Please give a Minimal, Complete, and Verifiable example in your question!
– jogo
Nov 21 '18 at 9:27
1
lapply(my_list,function(x) x[,colSums(is.na(x)) < 1, drop = FALSE])– Andre Elrico
Nov 21 '18 at 9:28
1
@RLave complete.cases looks row-wise for
NA. So not practical here– Andre Elrico
Nov 21 '18 at 9:38
3
For one matrix
apply(is.na(A), 2, any)gives you the logical index for the columns with NA. To remove the columns do:A[, !apply(is.na(A), 2, any)]. To do this with a list of matrices do:lapply(L, function(A) A[, !apply(is.na(A), 2, any)])– jogo
Nov 21 '18 at 10:02
1
Thank you, jogo, this solved it. I added an verifiable example to the question now for clarity
– Neuroguy
Nov 21 '18 at 10:11