absurd
absurd

Reputation: 1115

Split R dataframe according to number of cases per row

How do I split a dataframe like

a <- c("a","a","a","b","b","c","c","d","d","d")
b <- c(1,2,3,1,2,1,2,1,2,3)
df <- data.frame(a,b)

into single dataframes that contain only cases of equal length, i.e., all cases with three occurrences in a dataframe and all cases with two occurrences into a separate one?

The output should be:

dfa
a 1
a 2
a 3
d 1
d 2
d 3

dfb
b 1
b 2
c 1
c 2

Upvotes: 0

Views: 92

Answers (2)

Rich Scriven
Rich Scriven

Reputation: 99371

It's a bit more involved, but you could use droplevels with table

> tab <- table(df$a)
> lapply(3:2, function(x){
      droplevels(df[df$a %in% names(tab)[tab == x], , drop = FALSE])
  })
## [[1]]
##    a b
## 1  a 1
## 2  a 2
## 3  a 3
## 8  d 1
## 9  d 2
## 10 d 3

## [[2]]
##   a b
## 4 b 1
## 5 b 2
## 6 c 1
## 7 c 2

Upvotes: 0

sgibb
sgibb

Reputation: 25736

Have a look at ?split and ?ave:

split(df, ave(df$b, df$a, FUN = length))
#$`2`
#  a b
#4 b 1
#5 b 2
#6 c 1
#7 c 2
#
#$`3`
#   a b
#1  a 1
#2  a 2
#3  a 3
#8  d 1
#9  d 2
#10 d 3

Upvotes: 4

Related Questions