This question already has answers here:
How to calculate combination and permutation in R?
(6 answers)
Closed 8 years ago.
Consider I have a list of products : For example 100 product names
Product$list
A
B
C
I want 100 * 100 combination in Product$list1 and Product$list2
Example:3*3 = 9 combinations
Product$list1 Product$list2
A B
A C
B A
B C
C A
C B
A A
B B
C C
Can somebody help me how to achieve this in R.
You could try combn
setNames(as.data.frame(t(combn(Product$Col,2))), paste0("Col",1:2))
data
Product <- data.frame(Col=LETTERS[1:3], stringsAsFactors=FALSE)
Related
This question already has answers here:
How can I remove all duplicates so that NONE are left in a data frame?
(3 answers)
Finding ALL duplicate rows, including "elements with smaller subscripts"
(9 answers)
Closed 5 years ago.
I know this question has been asked in all sorts of variants, but I could not
extract the solution to my specific problem. Given a data frame like this:
a <- c(rep("A", 3), rep("B", 3), rep("C",2))
b <- c(1,1,2,4,1,1,2,2)
df <-data.frame(a,b)
This results in:
a b
1 A 1
2 A 1
3 A 2
4 B 4
5 B 1
6 B 1
7 C 2
8 C 2
I want to only keep row number 3 (A 2) and 4 (B 4).
I have tried all combinations of unique(), duplicated() and !duplicated() or
distinct, but could not get the desired result, since there seems to be no
combination of logical TRUE and FALSE that only filters out the non-duplicated rows. Thanks in advance!
This question already has answers here:
How to sum a variable by group
(18 answers)
Closed 5 years ago.
i have a database structure like this
A B C
n 1 M
n 2 U
n 1 U
f 3 M
f 4 M
f 1 U
using the package tidyr, I want to obtain this result:
A B C
n 1 M
n 3 U
f 7 M
f 1 U
So I want to make a sum of the b value characterized by the same A value and, obtained this sub set, collapsing B value in relation to the same C value.
How could I do?
library(dplyr)
df %>%
group_by(A,C) %>%
summarize(B=sum(B)) %>%
data.frame()
This question already has answers here:
Counting unique / distinct values by group in a data frame
(12 answers)
How to count the number of unique values by group? [duplicate]
(1 answer)
Closed 5 years ago.
I want to get the number of unique values from one column grouped by another column using dplyr. Preferable function friendly, that is i can put this in a function and it will work easily.
So for example for the following data frame.
test = data.frame(one=rep(letters[1:5],each=2), two=c(rep("c", 3), rep("d", 2), rep("e", 4), "f") )
one two
1 a c
2 a c
3 b c
4 b d
5 c d
6 c e
7 d e
8 d e
9 e e
10 e f
I would want something like the number of unique values column two gives column one.
Desired output:
one n
1 a 1
2 b 2
3 c 2
4 d 1
5 e 2
From column one, a has 1 unique value "c" only, b has 2 unique value "c" and "d", c has 2 unique values "d" and "e", d has 1 unique value "e".
I managed to get something working by group_by() twice and summarize(), is there a more simple way i could use?
Hope this is understandable.
Thanks
We can group by 'one' and get the number of unique elements with n_distinct
library(dplyr)
test %>%
group_by(one) %>%
summarise(n = n_distinct(two))
This question already has answers here:
Aggregating by unique identifier and concatenating related values into a string [duplicate]
(4 answers)
Closed 5 years ago.
My current dataset :
order product
1 a
1 b
1 c
2 b
2 d
3 a
3 c
3 e
what I want
product order
a 1,3
b 1,2
c 1,3
d 2
e 3
I have tried cast, reshape, but they didn't work
I recently spent way too much time trying to do something similar. What you need here, I believe, is a list-column. The code below will do that, but it turns the order number into a character value.
library(tidyverse)
df <- tibble(order=c(1,1,1,2,2,3,3,3), product=c('a','b','c','b','d','a','c','e')) %>%
group_by(product) %>%
summarise(order=toString(.$order)) %>%
mutate(order=str_split(order, ', ')
This question already has answers here:
How do you pivot data from a list of data frames in R?
(3 answers)
Closed 5 years ago.
I have some dataframes, I want to merged them if their first two columns are identical, and add the corresponding third column.
For example, I have three dataframe as follows:
> dump1
a b c
q 12 2
w 23 3
e 34 4
> dump2
a b c
q 12 1
w 23 1
s 3 1
> dump3
a b c
q 2 6
w 23 7
s 3 8
d 2 9
Now,I want to get the merged dataframe:
> dump5
a b c
d 2 9
q 2 6
s 3 9
q 12 3
w 23 11
e 34 4
The data is very big, so I want to have a quikly way.
How to do it? Anybody knows?
Appreciate in advance.
Thank you.
We place the datasets in a list, rbind it with rbindlist from data.table, grouped by 'a' and 'b', get the sum of 'c'
library(data.table)
rbindlist(list(dump1, dump2, dump3))[, .(c = sum(c)), .(a, b)]
If there are many datasets with object names start with dump followed by numbers created in the global environment, instead of specifying the object names individually, we can use ls with pattern to get the object names, and then values with mget in a list,
rbindlist(mget(ls(pattern = "dump\\d+")))[, .(c= sum(c)), .(a, b)]