-3

I have two 'data frames' that I'd like to append into a single csv file. I know that one can use write. table to append them, but write.table appends them vertically. Is there a way that I can append them horizontally?

Edit: The two data sets are 7x2 (which is constant), and Sx7 where S is variable. S can be very large, potentially up to 1000 or more.

Edit2: I want them to be arranged as with the Sx7 first, followed by a column of space, then the 7x2. I don't want them to be transposed as S can be very large and I'd like to be able to read them in LibreOffice/Excel.

Plinth
  • 289
  • 1
  • 13
  • 1
    See `cbind` and `rbind`. – zx8754 Sep 05 '14 at 19:37
  • The data.frames are of different sizes. – Plinth Sep 05 '14 at 19:45
  • @Plinth Try `merge` or `join` from `plyr` – akrun Sep 05 '14 at 19:47
  • How are these tables related? What exactly do you want to fill in the blank cells with then the sizes don't match? Please create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output. – MrFlick Sep 05 '14 at 20:32
  • @MrFlick I just edited to give the dimensions. – Plinth Sep 05 '14 at 20:46
  • @akrun I tried join but it does not give me what i'd like, namely the data frames do not both start from row 1. – Plinth Sep 05 '14 at 20:48
  • But that still doesn't scribe how you want them arranged in a single matrix (which is basically what a CSV file is). It's interesting that they both share a 7. Do you want to do a transpose and merge or something? What is the final desired layout?!? – MrFlick Sep 05 '14 at 20:48
  • I want them to be arranged as with the Sx7 first, followed by a column of space, then the 7x2. I don't want to transpose because S can be quite big and Libre Office may not be able to handle it. – Plinth Sep 05 '14 at 20:53

1 Answers1

1

This is definitely a quick hack.

Your problem with using cbind is that the dataframes aren't of the same size. A quick way to fix this is to make them all the same size, filling in the blank spaces with something, in my case below NAs. Then you can cbind all the dataframes and output them as one big dataframe to a file. This will give the appearance of them being added horizontally, of course with all the redundant fillers.

#create a list of the dataframes
dfList<-list(df1, df2)
#find the max rows
maxRows<-max(sapply(dfList, nrow))

#define a function that makes all dataframes "cbind-able"

equalRows<-function(df, numRows){
    numColumns<-ncol(df)
    targetRows<-numRows-nrow(df)
    toAppend<-as.data.frame(matrix(rep(NA, numColumns*targetRows), nrow=targetRows, ncol=numColumns))
    colnames(toAppend)<-colnames(df)
    rbind(df, toAppend)
}

#create our new cbind-able dataframes
newdfList<-lapply(dfList, function(df){

            if(nrow(df)==maxRows){
                df
            }else{
                equalRows(df, maxRows)
            }
        })

#put them all in one for output
dfResult<-newdfList[[1]]
for(i in 2:length(newdfList)){
    dfResult<-cbind(dfResult, newdfList[[i]])
}
DMT
  • 1,577
  • 10
  • 16
  • Thanks. A bit clunky but at least it works! – Plinth Sep 05 '14 at 20:52
  • @Plinth I'm sure there's a better way to do it, this probably won't scale well, and is definitely clunky! But hopefully it's a quick fix for a small problem. I'd pursue other avenues if this is something you're doing a lot for large amounts of data – DMT Sep 05 '14 at 21:23