1

I have data that looks like this and 1000 files with the same data format.

R_338   4
R_341   1
R_471   1
R_491   4
R_494   1
R_642   0
M_218   5
M_222   5
M_292   0
p_185   5
p_187   5
A_308   0
A_473   1

I would really appreciate if someone can direct me to as to how i can write a R script to so that i can merge all 1000 files by only keeping the first column once and the rest 0f the 1000 columns appended:

Example output:

R_338   4   5   6   7   8   9   10  11
R_341   1   1   1   1   1   1   1   1
R_471   1   1   0   1   1   1   2   1
R_491   4   4   4   4   4   4   2   0
R_494   1   1   1   1   1   1   1   1
R_642   0   1   0   9   1   1   2   1
M_218   5   5   5   9   5   5   5   9
M_222   5   5   5   5   5   5   5   5
M_292   0   5   1   1   1   1   1   1
p_185   5   5   5   6   5   5   5   5
p_187   5   9   5   5   5   5   3   5
A_308   0   4   4   4   2   4   4   4
A_473   1   1   1   1   0   1   1   0
3
  • It probably doesn't matter, but the second column 4,1,1,4,1,... in your example input has not been perfectly reproduced in the second column of your example output. Commented Jul 10, 2011 at 23:06
  • i did correct that.i did look at cbind but it is not adding columns to the final matrix...do you know the correct syntax... Commented Jul 10, 2011 at 23:14
  • it's not clear from your question whether the first "column" is really a separate column or whether it is the row names of an otherwise numeric data set ... Commented Jul 11, 2011 at 12:19

2 Answers 2

3

Suppose you have a character vector containing the file names. Then I think

L <- lapply(file.names,read.table,...)  ## where ... represents additional 
                                        ## arguments to read.table()
cbind(L[[1]],do.call(cbind,lapply(L[-1],"[[",2)))

might work.

Sign up to request clarification or add additional context in comments.

Comments

-1

It rather depends on the precise details of what you have, but cbind or merge are likely to provide you with what you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.