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 restructured 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
From stackoverflow
-
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?
Brandon Bertelsen : Why is it not recommended to have numbers as variable names?Shane : Eduardo is right. R doesn't allow you to store a variable starting with a number. Try this: 1b <- 1; b1 <- 1. If you ever attached your dataset, you could run into trouble.Brandon Bertelsen : Thanks for the explanation! Much appreciated
0 comments:
Post a Comment