Jamie_93
Jamie_93

Reputation: 35

count data with shared row

I'm facing with some data cleaning excercise and i have this type of data:

A<-c("A","A:::B","B")
B<-c("x","x","y")
df <- as.data.frame(cbind(A,B))
df

The example is:

      V1 V2
1     A  x
2 A:::B  x
3     B  y

I need this result:

     Var1Var2Count
1     A   x    2
2     B   x    1
3     B   y    1

I try some code like:

count<-aggregate(((unlist(strsplit(A,"\\:+"))))~B)

But i get error

Error in model.frame.default(formula = ((unlist(strsplit(A, : variable lengths differ (found for 'B')

This because i thing after the unlist vector A became longer than before. How can I do?

Thanks for help me.

Upvotes: 1

Views: 28

Answers (1)

Karthik S
Karthik S

Reputation: 11584

Does this work:

library(tidyr)
library(dplyr)
> df %>% separate_rows(A, sep = ':::') %>% count(A,B, name = 'Count')
  A B Count
1 A x     2
2 B x     1
3 B y     1
> 

Upvotes: 1

Related Questions