title: Double for loop to save several files using R
body: <p>I am trying to do a “for loop” to generate files based on the column "group". I want to create a file for each group.  My data is much bigger, but a sample would be:</p>
  
  <pre><code>id = c(1,2,3,4,5,6,7,8,9,10)
  group = c(3,1,3,2,1,3,1,2,4,4)
  weight = c(10,11,12,13,14,15,16,17,18,19)
  index1 = c(50,50,50,50,50,50,50,50,50,50)
  index2 = c(50,50,50,50,50,50,50,50,50,50)
  data = data.frame(id,group,weight,index1,index2)
  for (i in unique(data$group)){
  for (j in 1:nrow(data)){
  data$weight[j] = ifelse(data$group[j] == data$group[i], 0,data$weight[j])
  data$index1[j] = ifelse(data$group[j] == data$group[i], 0,50)
  data$index2[j] = ifelse(data$group[j] == data$group[i], 5,50)
  }
  write.table(data,paste("/home/paulaf/test/",data$group[i],".txt",sep=""),
                 quote=F,row.names=F,col.names=T)}
  </code></pre>
  
  <p>It seems to work, but it doesn’t write all the files. Any help would be very much appreciated. Thanks in advance.</p>