开发者

How to output the data with different lengths

开发者 https://www.devze.com 2023-02-19 16:59 出处:网络
R Version 2.11.1 32-bit on Windows 7 I\'m wondering if anyone else has encountered this question. I got several arrays with different lengths, and I want put them together to output. For example:

R Version 2.11.1 32-bit on Windows 7

I'm wondering if anyone else has encountered this question. I got several arrays with different lengths, and I want put them together to output. For example:

a=c(1,2,3);
b=c(2,4,1,6)
c=c(4,5,9,2,8)
ra=rank(a);#ra=1 2 3
rb=rank(b);#r开发者_如何学Pythonb=2 3 1 4
rc=rank(c);#rc=2 3 5 1 4

then how to put ra, rb and rc together to be this:

1 2 3

2 3 1 4

2 3 5 1 4

Yes, list() may be help, but how could I save it to my PC. I tred to use write.table(), but fail.


Transform list to matrix of strings.

a=c(1,2,3);
b=c(2,4,1,6)
c=c(4,5,9,2,8)

rlist <- lapply(list(a,b,c), rank)
m <- do.call(rbind, lapply(rlist, 
                           function(x) paste(x,collapse=" ")
                           )
             )
write.table(m,file="file_name")


Sounds like you want a list.

> list(ra, rb, rc)
[[1]]
[1] 1 2 3

[[2]]
[1] 2 3 1 4

[[3]]
[1] 2 3 5 1 4


> rlist <- lapply( list(a,b,c), rank)
> rlist
[[1]]
[1] 1 2 3

[[2]]
[1] 2 3 1 4

[[3]]
[1] 2 3 5 1 4
0

精彩评论

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