Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple data frames from one based off values with a for loop

I have a large data frame that I would like to convert in to smaller subset data frames using a for loop. I want the new data frames to be based on the the values in a column in the large/parent data frame. Here is an example

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

ok, now I want three data frames, one will be columns x and y but only where y == "A", the second where y== "B" etc etc. So the end result will be 3 new data frames df.A, df.B, and df.C. I realize that this would be easy to do out of a for loop but my actual data has a lot of levels of y so using a for loop (or similar) would be nice.

Thanks!

like image 697
wraymond Avatar asked Dec 05 '22 20:12

wraymond


2 Answers

If you want to create separate objects in a loop, you can use assign. I used unique because you said you had many levels.

 for(i in unique(df$y)) {
        nam <- paste("df", i, sep = ".")
        assign(nam, df[df$y==i,])
        }

> df.A
  x y
1 1 A
2 2 A
3 3 A
4 4 A
5 5 A
6 6 A
7 7 A
8 8 A
> df.B
    x y
9   9 B
10 10 B
11 11 B
12 12 B
13 13 B
14 14 B
like image 199
Pierre Lapointe Avatar answered Mar 15 '23 14:03

Pierre Lapointe


I think you just need the split function:

 split(df, df$y)
$A
  x y
1 1 A
2 2 A
3 3 A
4 4 A
5 5 A
6 6 A
7 7 A
8 8 A

$B
    x y
9   9 B
10 10 B
11 11 B
12 12 B
13 13 B
14 14 B
15 15 B
16 16 B
17 17 B

$C
    x y
18 18 C
19 19 C
20 20 C

It is just a matter of properly subsetting the output to split and store the results to objects like dfA <- split(df, df$y)[[1]] and dfB <- split(df, df$y)[[2]] and so on.

like image 26
SabDeM Avatar answered Mar 15 '23 15:03

SabDeM