1
votes

I try to extract a result like this:

My first attempt is this data as I saw table() brings the frequencies

However I can observe in the results that it is not possible to identify that the frequency is the same if they are in different order as the example the result I try to extract.

values, frq
"Google, Yahoo", 2
"Amazon, Google", 2

How can I solve it?

Should I first capture the unique names like "Google", "Yahoo", "Amazon". After that convert the data.frame to one with 4 columns one id which saws every single row and the other three has the names of columns and receive 1 if the have this value? Is there any simpler way to do it?

table(data.frame(stock = c("Google, Yahoo", "Google", "Yahoo, Google", "Amazon, Google", "Google, Amazon")))

Amazon, Google         Google Google, Amazon  Google, Yahoo  Yahoo, Google 
             1              1              1              1              1
1
Try table(sapply(strsplit(as.character(df1$stock), ", "), function(x) toString(sort(x))))akrun
@akrun thank you you are right!Nathalie

1 Answers

1
votes

We split the 'stock' by the delimiter, sort and paste it together, then use the table

table(sapply(strsplit(as.character(df1$stock), ", "), function(x) toString(sort(x))))

data

df1 <- data.frame(stock = c("Google, Yahoo", "Google", "Yahoo, Google", 
     "Amazon, Google", "Google, Amazon"))