开发者

Data manipulation with R: Restructuring Data

I have a dataset that looks like this:

a <- data.frame(rep(1,5),1:5,1:5)
b <- data.frame(rep(2,5),1:5,1:5)
colnames(a) <- c(1,2,3)
colnames(b) <- c(1,2,3)
c <- rbind(a,b)

   1 2 3
1  1 1 1
2  1 2 2
3  1 3 3
4  1 4 4
5  1 5 5
6  2 1 1
7  2 2 2
8  2 3 3
9  2 4 4
10 2 5 5

but I want it to be restruct开发者_开发技巧ured to this:

    2_1 2_2 3_1 3_2
   1  1   1   1   1
   2  2   2   2   2 
   3  3   3   3   4
   4  4   4   4   4 
   5  5   5   5   5


a <- data.frame(rep(1,5),1:5,1:5)
b <- data.frame(rep(2,5),1:5,1:5)
colnames(b) <- colnames(a) <- paste("a", c(1,2,3), sep='')
d <- rbind(a,b)
library(reshape)
recast(d, a2 ~ a1, measure.var="a3")

I changed your example slightly, since it had numbers as variable names. This is not recommended because it permits the following nonsense:

 "1" <- 3
print(1)
[1] 1
print("1")
[1] "1"
print(`1`)
[1] 3

Need I say more?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜