Count vectors in list with the same elements in r
I have a list of ~8000 vectors, and I would like to know how many duplicates there are of these 8000 vectors, but the order of the elements in each could be different.
for example:
list <- c()
list[[1]] <- c(1,2,3)
list[[2]] <- c(2,1,3)
list[[3]] <- c(3,2,1)
list[[4]] <- c(4,5)
list[[5]] <- c(5,4)
list[[6]] <- c(1,2,3,5)
should give me a count of 3 for c(1,2,3)
and 2 for c(4,5)
and 1 for c(1,2,3,5)
I'd like the count of each of the duplicates, not just how many are duplicated.
r list
add a comment |
I have a list of ~8000 vectors, and I would like to know how many duplicates there are of these 8000 vectors, but the order of the elements in each could be different.
for example:
list <- c()
list[[1]] <- c(1,2,3)
list[[2]] <- c(2,1,3)
list[[3]] <- c(3,2,1)
list[[4]] <- c(4,5)
list[[5]] <- c(5,4)
list[[6]] <- c(1,2,3,5)
should give me a count of 3 for c(1,2,3)
and 2 for c(4,5)
and 1 for c(1,2,3,5)
I'd like the count of each of the duplicates, not just how many are duplicated.
r list
1
Do you needlength(unique(list))
Also, if you need all the duplicates thensum(duplicated(lst)|duplicated(lst, fromLast = TRUE))
– akrun
Nov 16 '18 at 19:54
in a sense, but I would like some sort of function that tells me which list was duplicated how many times, in the example above it would bec(1,2,3) = 3
or something like that
– HibaShaban
Nov 16 '18 at 20:00
1
What you are showing is not a list of lists but a list of vectors
– prosoitos
Nov 16 '18 at 20:05
1
You have a typo:List
should belist
on your first line
– prosoitos
Nov 16 '18 at 20:38
1
yes, thanks. Was trying to make it reproducible but that part slipped through.
– HibaShaban
Nov 16 '18 at 20:41
add a comment |
I have a list of ~8000 vectors, and I would like to know how many duplicates there are of these 8000 vectors, but the order of the elements in each could be different.
for example:
list <- c()
list[[1]] <- c(1,2,3)
list[[2]] <- c(2,1,3)
list[[3]] <- c(3,2,1)
list[[4]] <- c(4,5)
list[[5]] <- c(5,4)
list[[6]] <- c(1,2,3,5)
should give me a count of 3 for c(1,2,3)
and 2 for c(4,5)
and 1 for c(1,2,3,5)
I'd like the count of each of the duplicates, not just how many are duplicated.
r list
I have a list of ~8000 vectors, and I would like to know how many duplicates there are of these 8000 vectors, but the order of the elements in each could be different.
for example:
list <- c()
list[[1]] <- c(1,2,3)
list[[2]] <- c(2,1,3)
list[[3]] <- c(3,2,1)
list[[4]] <- c(4,5)
list[[5]] <- c(5,4)
list[[6]] <- c(1,2,3,5)
should give me a count of 3 for c(1,2,3)
and 2 for c(4,5)
and 1 for c(1,2,3,5)
I'd like the count of each of the duplicates, not just how many are duplicated.
r list
r list
edited Nov 18 '18 at 13:15
asked Nov 16 '18 at 19:52
HibaShaban
20110
20110
1
Do you needlength(unique(list))
Also, if you need all the duplicates thensum(duplicated(lst)|duplicated(lst, fromLast = TRUE))
– akrun
Nov 16 '18 at 19:54
in a sense, but I would like some sort of function that tells me which list was duplicated how many times, in the example above it would bec(1,2,3) = 3
or something like that
– HibaShaban
Nov 16 '18 at 20:00
1
What you are showing is not a list of lists but a list of vectors
– prosoitos
Nov 16 '18 at 20:05
1
You have a typo:List
should belist
on your first line
– prosoitos
Nov 16 '18 at 20:38
1
yes, thanks. Was trying to make it reproducible but that part slipped through.
– HibaShaban
Nov 16 '18 at 20:41
add a comment |
1
Do you needlength(unique(list))
Also, if you need all the duplicates thensum(duplicated(lst)|duplicated(lst, fromLast = TRUE))
– akrun
Nov 16 '18 at 19:54
in a sense, but I would like some sort of function that tells me which list was duplicated how many times, in the example above it would bec(1,2,3) = 3
or something like that
– HibaShaban
Nov 16 '18 at 20:00
1
What you are showing is not a list of lists but a list of vectors
– prosoitos
Nov 16 '18 at 20:05
1
You have a typo:List
should belist
on your first line
– prosoitos
Nov 16 '18 at 20:38
1
yes, thanks. Was trying to make it reproducible but that part slipped through.
– HibaShaban
Nov 16 '18 at 20:41
1
1
Do you need
length(unique(list))
Also, if you need all the duplicates then sum(duplicated(lst)|duplicated(lst, fromLast = TRUE))
– akrun
Nov 16 '18 at 19:54
Do you need
length(unique(list))
Also, if you need all the duplicates then sum(duplicated(lst)|duplicated(lst, fromLast = TRUE))
– akrun
Nov 16 '18 at 19:54
in a sense, but I would like some sort of function that tells me which list was duplicated how many times, in the example above it would be
c(1,2,3) = 3
or something like that– HibaShaban
Nov 16 '18 at 20:00
in a sense, but I would like some sort of function that tells me which list was duplicated how many times, in the example above it would be
c(1,2,3) = 3
or something like that– HibaShaban
Nov 16 '18 at 20:00
1
1
What you are showing is not a list of lists but a list of vectors
– prosoitos
Nov 16 '18 at 20:05
What you are showing is not a list of lists but a list of vectors
– prosoitos
Nov 16 '18 at 20:05
1
1
You have a typo:
List
should be list
on your first line– prosoitos
Nov 16 '18 at 20:38
You have a typo:
List
should be list
on your first line– prosoitos
Nov 16 '18 at 20:38
1
1
yes, thanks. Was trying to make it reproducible but that part slipped through.
– HibaShaban
Nov 16 '18 at 20:41
yes, thanks. Was trying to make it reproducible but that part slipped through.
– HibaShaban
Nov 16 '18 at 20:41
add a comment |
2 Answers
2
active
oldest
votes
library(tidyverse)
library(gtools)
get_perm <- function(v) {
m <- permutations(n = length(v), r = length(v), v = v, set = F)
m[order(c(m))]
}
all <- map(list, get_perm)
unique <- map(list, get_perm) %>% unique()
res_vec <- c()
element <- c()
for(i in seq_along(unique)) {
element[[i]] <- unique[[i]] %>% unique() %>% paste(collapse = ",")
res_vec[[i]] <- all %in% unique[i] %>% sum()
}
tibble(
elements = unlist(element),
numbers = res_vec
)
Result
# A tibble: 3 x 2
elements numbers
<chr> <int>
1 1,2,3 3
2 4,5 2
3 1,2,3,5 1
elements
contains all the individual elements of the vectors for each group and numbers
are the numbers of vectors you have in each group.
add a comment |
We create a function to take vector
as an argument ('val'), then loop through the list
with sapply
, check if all
the 'valare
%in%the 'x', and
sumthe logical
vector`
f1 <- function(lst, val) sum(sapply(lst, function(x) all(val %in% x)))
f1(list, c(1, 2, 3))
[#1] 3
f1(list, c(4, 5))
#[1] 2
1
Thanks! This worked
– HibaShaban
Nov 16 '18 at 20:34
So actually this half worked.. I'm not getting extra counts when the vectors contain more elements, examplec<-(1,2,3,5)
is being counted withc(1,2,3)
how would I fix that?
– HibaShaban
Nov 16 '18 at 22:00
I've resorted to sorting the vectors and then using == instead of %in%. that's the only way I can think of for now. Would appreciate any other solutions.
– HibaShaban
Nov 16 '18 at 22:51
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%2f53344497%2fcount-vectors-in-list-with-the-same-elements-in-r%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
library(tidyverse)
library(gtools)
get_perm <- function(v) {
m <- permutations(n = length(v), r = length(v), v = v, set = F)
m[order(c(m))]
}
all <- map(list, get_perm)
unique <- map(list, get_perm) %>% unique()
res_vec <- c()
element <- c()
for(i in seq_along(unique)) {
element[[i]] <- unique[[i]] %>% unique() %>% paste(collapse = ",")
res_vec[[i]] <- all %in% unique[i] %>% sum()
}
tibble(
elements = unlist(element),
numbers = res_vec
)
Result
# A tibble: 3 x 2
elements numbers
<chr> <int>
1 1,2,3 3
2 4,5 2
3 1,2,3,5 1
elements
contains all the individual elements of the vectors for each group and numbers
are the numbers of vectors you have in each group.
add a comment |
library(tidyverse)
library(gtools)
get_perm <- function(v) {
m <- permutations(n = length(v), r = length(v), v = v, set = F)
m[order(c(m))]
}
all <- map(list, get_perm)
unique <- map(list, get_perm) %>% unique()
res_vec <- c()
element <- c()
for(i in seq_along(unique)) {
element[[i]] <- unique[[i]] %>% unique() %>% paste(collapse = ",")
res_vec[[i]] <- all %in% unique[i] %>% sum()
}
tibble(
elements = unlist(element),
numbers = res_vec
)
Result
# A tibble: 3 x 2
elements numbers
<chr> <int>
1 1,2,3 3
2 4,5 2
3 1,2,3,5 1
elements
contains all the individual elements of the vectors for each group and numbers
are the numbers of vectors you have in each group.
add a comment |
library(tidyverse)
library(gtools)
get_perm <- function(v) {
m <- permutations(n = length(v), r = length(v), v = v, set = F)
m[order(c(m))]
}
all <- map(list, get_perm)
unique <- map(list, get_perm) %>% unique()
res_vec <- c()
element <- c()
for(i in seq_along(unique)) {
element[[i]] <- unique[[i]] %>% unique() %>% paste(collapse = ",")
res_vec[[i]] <- all %in% unique[i] %>% sum()
}
tibble(
elements = unlist(element),
numbers = res_vec
)
Result
# A tibble: 3 x 2
elements numbers
<chr> <int>
1 1,2,3 3
2 4,5 2
3 1,2,3,5 1
elements
contains all the individual elements of the vectors for each group and numbers
are the numbers of vectors you have in each group.
library(tidyverse)
library(gtools)
get_perm <- function(v) {
m <- permutations(n = length(v), r = length(v), v = v, set = F)
m[order(c(m))]
}
all <- map(list, get_perm)
unique <- map(list, get_perm) %>% unique()
res_vec <- c()
element <- c()
for(i in seq_along(unique)) {
element[[i]] <- unique[[i]] %>% unique() %>% paste(collapse = ",")
res_vec[[i]] <- all %in% unique[i] %>% sum()
}
tibble(
elements = unlist(element),
numbers = res_vec
)
Result
# A tibble: 3 x 2
elements numbers
<chr> <int>
1 1,2,3 3
2 4,5 2
3 1,2,3,5 1
elements
contains all the individual elements of the vectors for each group and numbers
are the numbers of vectors you have in each group.
edited Nov 17 '18 at 5:46
answered Nov 17 '18 at 0:47
prosoitos
935219
935219
add a comment |
add a comment |
We create a function to take vector
as an argument ('val'), then loop through the list
with sapply
, check if all
the 'valare
%in%the 'x', and
sumthe logical
vector`
f1 <- function(lst, val) sum(sapply(lst, function(x) all(val %in% x)))
f1(list, c(1, 2, 3))
[#1] 3
f1(list, c(4, 5))
#[1] 2
1
Thanks! This worked
– HibaShaban
Nov 16 '18 at 20:34
So actually this half worked.. I'm not getting extra counts when the vectors contain more elements, examplec<-(1,2,3,5)
is being counted withc(1,2,3)
how would I fix that?
– HibaShaban
Nov 16 '18 at 22:00
I've resorted to sorting the vectors and then using == instead of %in%. that's the only way I can think of for now. Would appreciate any other solutions.
– HibaShaban
Nov 16 '18 at 22:51
add a comment |
We create a function to take vector
as an argument ('val'), then loop through the list
with sapply
, check if all
the 'valare
%in%the 'x', and
sumthe logical
vector`
f1 <- function(lst, val) sum(sapply(lst, function(x) all(val %in% x)))
f1(list, c(1, 2, 3))
[#1] 3
f1(list, c(4, 5))
#[1] 2
1
Thanks! This worked
– HibaShaban
Nov 16 '18 at 20:34
So actually this half worked.. I'm not getting extra counts when the vectors contain more elements, examplec<-(1,2,3,5)
is being counted withc(1,2,3)
how would I fix that?
– HibaShaban
Nov 16 '18 at 22:00
I've resorted to sorting the vectors and then using == instead of %in%. that's the only way I can think of for now. Would appreciate any other solutions.
– HibaShaban
Nov 16 '18 at 22:51
add a comment |
We create a function to take vector
as an argument ('val'), then loop through the list
with sapply
, check if all
the 'valare
%in%the 'x', and
sumthe logical
vector`
f1 <- function(lst, val) sum(sapply(lst, function(x) all(val %in% x)))
f1(list, c(1, 2, 3))
[#1] 3
f1(list, c(4, 5))
#[1] 2
We create a function to take vector
as an argument ('val'), then loop through the list
with sapply
, check if all
the 'valare
%in%the 'x', and
sumthe logical
vector`
f1 <- function(lst, val) sum(sapply(lst, function(x) all(val %in% x)))
f1(list, c(1, 2, 3))
[#1] 3
f1(list, c(4, 5))
#[1] 2
edited Nov 16 '18 at 20:30
answered Nov 16 '18 at 20:02
akrun
398k13187260
398k13187260
1
Thanks! This worked
– HibaShaban
Nov 16 '18 at 20:34
So actually this half worked.. I'm not getting extra counts when the vectors contain more elements, examplec<-(1,2,3,5)
is being counted withc(1,2,3)
how would I fix that?
– HibaShaban
Nov 16 '18 at 22:00
I've resorted to sorting the vectors and then using == instead of %in%. that's the only way I can think of for now. Would appreciate any other solutions.
– HibaShaban
Nov 16 '18 at 22:51
add a comment |
1
Thanks! This worked
– HibaShaban
Nov 16 '18 at 20:34
So actually this half worked.. I'm not getting extra counts when the vectors contain more elements, examplec<-(1,2,3,5)
is being counted withc(1,2,3)
how would I fix that?
– HibaShaban
Nov 16 '18 at 22:00
I've resorted to sorting the vectors and then using == instead of %in%. that's the only way I can think of for now. Would appreciate any other solutions.
– HibaShaban
Nov 16 '18 at 22:51
1
1
Thanks! This worked
– HibaShaban
Nov 16 '18 at 20:34
Thanks! This worked
– HibaShaban
Nov 16 '18 at 20:34
So actually this half worked.. I'm not getting extra counts when the vectors contain more elements, example
c<-(1,2,3,5)
is being counted with c(1,2,3)
how would I fix that?– HibaShaban
Nov 16 '18 at 22:00
So actually this half worked.. I'm not getting extra counts when the vectors contain more elements, example
c<-(1,2,3,5)
is being counted with c(1,2,3)
how would I fix that?– HibaShaban
Nov 16 '18 at 22:00
I've resorted to sorting the vectors and then using == instead of %in%. that's the only way I can think of for now. Would appreciate any other solutions.
– HibaShaban
Nov 16 '18 at 22:51
I've resorted to sorting the vectors and then using == instead of %in%. that's the only way I can think of for now. Would appreciate any other solutions.
– HibaShaban
Nov 16 '18 at 22:51
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53344497%2fcount-vectors-in-list-with-the-same-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
1
Do you need
length(unique(list))
Also, if you need all the duplicates thensum(duplicated(lst)|duplicated(lst, fromLast = TRUE))
– akrun
Nov 16 '18 at 19:54
in a sense, but I would like some sort of function that tells me which list was duplicated how many times, in the example above it would be
c(1,2,3) = 3
or something like that– HibaShaban
Nov 16 '18 at 20:00
1
What you are showing is not a list of lists but a list of vectors
– prosoitos
Nov 16 '18 at 20:05
1
You have a typo:
List
should belist
on your first line– prosoitos
Nov 16 '18 at 20:38
1
yes, thanks. Was trying to make it reproducible but that part slipped through.
– HibaShaban
Nov 16 '18 at 20:41