0
stock_reco<- read.csv("C:\\temp\\Migration.csv")
migrate<-as.matrix(stock_reco)
title<-colnames(migrate)
dt1<-migrate[,3]
dt2<-as.Date(dt1, format= "%d/%m/%Y")
reco1<-migrate[,6]
reco<-as.matrix(reco1)
for(i in 1:4099)
{
  if((migrate[i,1]== migrate[i+1,1]) && (migrate[i,2]== migrate[i+1,2]))
    {
       k<-difftime(dt2[i+1],dt2[i],units = "days")
       if((k <=180) && (reco[i] == reco[i+1]))
       print (migrate[i,])
       print (migrate[i+1,])
       print ("----------------------------------------------------") 
    }

}

Last two print statements give my final output in the image attached below. I want the whole Final output in an excel file. So how do I import this output from R to an excel file? I want the output in this form

Comp Name Brokerage House Date CMP Target Recomendation Yes Bank Motilal Oswal 14/6/2011 294 420 Buy Yes Bank Motilal Oswal 22/9/2011 285 400 Buy

The above is just one set where if conditions are satisfied and there are several such pairs. I need all of these in an Excel sheet. When I run my code I get this in zig zag/ random form in R there are no errors in the code. Please let me know how to write this output to an excel sheet in detail. Thanks

pnuts
  • 58,317
  • 11
  • 87
  • 139
IceIceIce
  • 5
  • 1
  • 3
  • write.csv or write.csv2 . Then open csv in excel. Are you looking to do this in a more dynamic way? There are probably a thousand ways to do this but it all depends on your currect structure/needs – JohnandLyn Henry May 29 '15 at 11:59
  • I just want to do it in a simpler way. Can you please help me by using the exact commands according to the code. Could You write the syntax using write.csv for my code. Because when i tried i got errors. – IceIceIce May 29 '15 at 12:33
  • as per (http://stackoverflow.com/questions/2563824/writing-to-a-dataframe-from-a-for-loop-in-r) answer something like: d <- data.frame() for (i in 1:20) {d <- (use this in replacement of your for loop with the logic switched over)} then write to the csv with this other answer (http://stackoverflow.com/questions/15956183/how-to-save-a-data-frame-as-csv-to-a-user-selected-location-using-tcltk) so the command would be something like write.csv(file=fileName, x='d') – JohnandLyn Henry May 29 '15 at 12:35
  • @JohnandLynHenry comments are best used for giving short explanations. Consider investing some time and give the poster a structured answer. – Roman Luštrik May 29 '15 at 12:48
  • @IceIceIce please post a reproducible example and show us what the output should look like. You can use functions to write a table to excel file. See http://www.thertrader.com/2014/02/11/a-million-ways-to-connect-r-and-excel/ for some examples. – Roman Luštrik May 29 '15 at 12:49
  • @Roman Luštrik I have edited the post but there is a problem in formatting I want the output in sequential order like a tabular one. Please let me know one simple way to import my output into excel sheet in detail. – IceIceIce May 29 '15 at 13:18

1 Answers1

0

@iceiceice you could try this

stock_reco<- read.csv("C:\\temp\\Migration.csv")
migrate<-as.matrix(stock_reco)
title<-colnames(migrate)
dt1<-migrate[,3]
dt2<-as.Date(dt1, format= "%d/%m/%Y")
reco1<-migrate[,6]
reco<-as.matrix(reco1)
for (i in 1:4099) {
  if((migrate[i,1]== migrate[i+1,1]) && (migrate[i,2]== migrate[i+1,2]))
    {
       k<-difftime(dt2[i+1],dt2[i],units = "days")
       if((k <=180) && (reco[i] == reco[i+1]))
       d <- rbind(d, data.frame(migrate[i,], migrate[i+1,]))
        }
    }

write.csv(file=fileName, x='d')  
JohnandLyn Henry
  • 277
  • 1
  • 3
  • 11