Rui Z.
Rui Z.

Reputation: 47

Split a data frame by rows and save as csv

I just have a data frame and want to split the data frame by rows, assign the several new data frames to new variables and save them as csv files.

a <- rep(1:5,each=3)
b <-rep(1:3,each=5) 
c <- data.frame(a,b)
#  a b
1  1 1
2  1 1
3  1 1
4  2 1
5  2 1
6  2 2
7  3 2
8  3 2
9  3 2
10 4 2
11 4 3
12 4 3
13 5 3
14 5 3
15 5 3

I want to split c by column a. i.e all rows are 1 in column a are split from c and assign it to A and save A as A.csv. The same to B.csv with all 2 in column a. What I can do is

 A<-c[c$a%in%1,]
 write.csv (A, "A.csv")
 B<-c[c$a%in%2,]
 write.csv (B, "B.csv") 
 ...

If I have 1000 rows and there will be lots of subsets, I just wonder if there is a simple way to do this by using for loop?

Upvotes: 3

Views: 3279

Answers (2)

nghauran
nghauran

Reputation: 6768

The split() function is very useful to split data frame. Also, you can use lapply() here - it should be more efficient than a loop.

dfs <- split(c, c$a) # list of dfs
# use numbers as file names
lapply(names(dfs),
       function(x){write.csv(dfs[[x]], paste0(x,".csv"),
                             row.names = FALSE)}) 
# or use letters (max 26!) as file names
names(dfs) <- LETTERS[1:length(dfs)]
lapply(names(dfs),
       function(x){write.csv(dfs[[x]],
                             file = paste0(x,".csv"),
                             row.names = FALSE)})

Upvotes: 3

David Klotz
David Klotz

Reputation: 2431

for(i in seq_along(unique(c$a))){
  write.csv(c[c$a == i,], paste0(LETTERS[i], ".csv"))}

You should consider, however, what happens if you have more than 26 subsets. What will those files be named?

Upvotes: 1

Related Questions