Hi Rusers, I hope to divide the original dataset into several subsets and output these multilple datasets. But errors appeared in my loops. See example. ###### a<-c(1:10) b<-c(rep(1,3),rep(2,3),rep(3,4)) c<-data.frame(a,b) #c is the example data num<-c(unique(b)) # I hope to get the subsets c_1.csv,c_2.csv and c_3.csv #Errors for (i in num) { c_num<-c[c$b==num,] write.csv(c_num,file="c:/c_num.csv") } Warning messages: 1: In c$b == num : longer object length is not a multiple of shorter object length 2: In c$b == num : longer object length is not a multiple of shorter object length 3: In c$b == num : longer object length is not a multiple of shorter object length I think the problem should be file="c:/c_num.csv", anybody has ever met this problem? Thanks very much. ----------------- Jane Chang Queen's [[alternative HTML version deleted]]
Have you considered using split? -Ista On Sun, Nov 8, 2009 at 7:23 PM, rusers.sh <rusers.sh at gmail.com> wrote:> Hi Rusers, > ?I hope to divide the original dataset into several subsets and output > these multilple datasets. But errors appeared in my loops. See example. > ###### > a<-c(1:10) > b<-c(rep(1,3),rep(2,3),rep(3,4)) > c<-data.frame(a,b) ?#c is the example data > num<-c(unique(b)) > # I hope to get the subsets c_1.csv,c_2.csv and c_3.csv > #Errors > for (i in num) ?{ > ? c_num<-c[c$b==num,] > ? write.csv(c_num,file="c:/c_num.csv") > } > > Warning messages: > 1: In c$b == num : > ?longer object length is not a multiple of shorter object length > 2: In c$b == num : > ?longer object length is not a multiple of shorter object length > 3: In c$b == num : > ?longer object length is not a multiple of shorter object length > ?I think the problem should be file="c:/c_num.csv", anybody has ever met > this problem? > ?Thanks very much. > ----------------- > Jane Chang > Queen's > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
On Nov 8, 2009, at 7:23 PM, rusers.sh wrote:> for (i in num) { > c_num<-c[c$b==num,] > write.csv(c_num,file="c:/c_num.csv") > } > > Warning messages: > 1: In c$b == num : > longer object length is not a multiple of shorter object lengthThis is because you're comparing column b to the entire vector of numbers (num), not the current number in the iteration (i). The first line of the loop should be "c_num<-c[c$b==i,]". From a style point of view, I'd use "n" as my variable, since "i" is too commonly used as an integer index. Also, you will be overwriting the same file, called "c_num.csv", on each iteration. You should try something more like: for (n in num) { c.n <- c[c$b==n,] write.csv(c.n, file=paste("c:/c_", n, ".csv", sep="") } I hope that helps. Cheers, Johann Hibschman