order
order

Reputation: 375

Storing output of read.table in a data structure

I have tried using vectors, arrays, lists and collections. When I go to access the data later it seems that the data is cast into some one dimensional structure. For example.

This:

for (i in 1:length(argv)){
    temp= read.csv(file=argv[i], header= TRUE)   
    print( temp )
    input_tables[i]= temp
    print(input_tables[i])

Yields this:

     V1 V2 V3 V4
1  1  5  9 13
2  2  6 10 14
3  3  7 11 15
4  4  8 12 16
[[1]]
[1] 1 2 3 4

  V1 V2 V3 V4
1  2 10 18 26
2  4 12 20 28
3  6 14 22 30
4  8 16 24 32
[[1]]
[1] 2 4 6 8

  V1 V2 V3 V4
1  4 20 36 52
2  8 24 40 56
3 12 28 44 60
4 16 32 48 64
[[1]]
[1]  4  8 12 16

I have looked through the documentation for R (a bit of googling and the help function) and I cant determine what sort of object read.table returns. From my previous experience, I believe that is a dataframe or a matrix.

In any case, how can i store these outputs in some structure and not cast it? Or at least not discard the data. It may be there as it is now. Thanks for your time. I have a workaround in my current situation, but it seems like this should be easily possible.

Upvotes: 0

Views: 652

Answers (1)

mnel
mnel

Reputation: 115485

read.csv will return a data.frame (as stated in ?read.csv, under value)

Two approaches to combine multiple files, assuming argv is a vector of file names and that the inputs will have the same number and order of columns.

.list <- lapply(argv, read.csv, header = T)
all_data <- do.call(rbind, .list)

Or, using plyr

library(plyr)
all_data <- ldply(argv, read.csv, header = T)

Upvotes: 5

Related Questions