开发者

R-how to add columns to a data frame in a for loop?

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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜