1

I've a db like this

v1 <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
v2 <- c(2.2, 3.2, 1.2, 4.2, 2.2, 3.2, 2.2, 1.2, 5.2)
v3 <- c("a","a","a","b","b","b","c","c","c")
d <- data.frame(v1,v2,v3)

I would like to create subsets of d basing on unique values of v1. Can anyone help?

www
  • 38,575
  • 12
  • 48
  • 84
sarovasta
  • 89
  • 6

1 Answers1

1

You can use the split-function for that:

split(d, d$v1)

The result:

> split(d, d$v1)
$`1`
  v1  v2 v3
1  1 2.2  a
2  1 3.2  a
3  1 1.2  a

$`2`
  v1  v2 v3
4  2 4.2  b
5  2 2.2  b
6  2 3.2  b

$`3`
  v1  v2 v3
7  3 2.2  c
8  3 1.2  c
9  3 5.2  c
h3rm4n
  • 4,126
  • 15
  • 21
  • Thanks! But then, how to apply the same function (for example rowSums(subset[1:10]) to all dataframes split created? – sarovasta Nov 26 '17 at 15:13
  • @sarovasta you can use `lapply` fro that as `split` gives a list back – h3rm4n Nov 26 '17 at 16:35
  • Sorry, didn't get it! Could you explain it using the specific example? – sarovasta Nov 26 '17 at 17:04
  • @sarovasta for example `lapply(split(d, d$v1), function(x) colSums(x[1:2]))`; for further reading [see here](https://stackoverflow.com/questions/3505701/grouping-functions-tapply-by-aggregate-and-the-apply-family) – h3rm4n Nov 26 '17 at 18:18