I have the following:
Time A1 A1 B1 B1 C1 C2
x y x y x y
0 5 6 6 7 7 9
1 3 4 4 3 9 9
2 5 2 6 4 7 4
I want to change it to the following:
0 1 2
x y x y x y
A1 5 6 3 4 5 2
B1 6 7 4 3 6 4
etc for a much larger set
I am sure there are ways to accomplish through a lot of for loops but I feel
there is most likely a function to use.
Thank you.
--
View this message in context: http://r.789695.n4.nabble.com/Breaking-up-a-Row-in-R-transpose-tp4607658.html
Sent from the R help mailing list archive at Nabble.com.
Breaking up a Row in R (transpose)
7 messages · Petr Savicky, marc212, Rui Barradas
On Thu, May 03, 2012 at 07:36:45PM -0700, marc212 wrote:
I have the following:
Time A1 A1 B1 B1 C1 C2
x y x y x y
0 5 6 6 7 7 9
1 3 4 4 3 9 9
2 5 2 6 4 7 4
I want to change it to the following:
0 1 2
x y x y x y
A1 5 6 3 4 5 2
B1 6 7 4 3 6 4
etc for a much larger set
Hi.
Try the following.
# the example input
Orig <- rbind(
"0"=c(5, 6, 6, 7, 7, 9),
"1"=c(3, 4, 4, 3, 9, 9),
"2"=c(5, 2, 6, 4, 7, 4))
colnames(Orig) <- rep(c("x", "y"), times=3)
# transformation
Arr <- array(Orig, dim=c(nrow(Orig), 2, ncol(Orig)/2))
Arr
, , 1 # this is the required row 1 in the output
[,1] [,2]
[1,] 5 6
[2,] 3 4
[3,] 5 2
, , 2 # this is the required row 2 in the output
[,1] [,2]
[1,] 6 7
[2,] 4 3
[3,] 6 4
, , 3
[,1] [,2]
[1,] 7 9
[2,] 9 9
[3,] 7 4
Arr1 <- aperm(Arr, perm=c(2, 1, 3))
d <- dim(Arr1)
t(array(Arr1, dim=c(d[1]*d[2], d[3])))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 5 6 3 4 5 2
[2,] 6 7 4 3 6 4
[3,] 7 9 9 9 7 4
Hope this helps.
Petr Savicky.
I have something like 28 rows and 6000 columns. How would I configure this with for loops. Thank you. Marc -- View this message in context: http://r.789695.n4.nabble.com/Breaking-up-a-Row-in-R-transpose-tp4607658p4609309.html Sent from the R help mailing list archive at Nabble.com.
Hello, marc212 wrote
I have the following:
Time A1 A1 B1 B1 C1 C2
x y x y x y
0 5 6 6 7 7 9
1 3 4 4 3 9 9
2 5 2 6 4 7 4
I want to change it to the following:
0 1 2
x y x y x y
A1 5 6 3 4 5 2
B1 6 7 4 3 6 4
etc for a much larger set
I am sure there are ways to accomplish through a lot of for loops but I
feel there is most likely a function to use.
Thank you.
Using Petr's Orig data.frame above, maybe this is what you want.
cnames <- unique(colnames(Orig))
nc <- ncol(Orig)/length(cnames)
res <- lapply(seq.int(nrow(Orig)), function(i){
x <- Orig[i, ]
dim(x) <- c(length(cnames), nc)
dimnames(x) <- list(cnames, LETTERS[seq_len(nc)])
t(x)
})
names(res) <- rownames(Orig)
res
do.call(cbind, res)
And it avoids loops.
Hope this helps,
Rui Barradas
--
View this message in context: http://r.789695.n4.nabble.com/Breaking-up-a-Row-in-R-transpose-tp4607658p4609488.html
Sent from the R help mailing list archive at Nabble.com.
I am not opposed to for loops just do not know how to implement them.
I am sorry for all the questions I am trying to learn. The posted code does
not work for me in my set I am getting a :
Error in `dimnames<-.data.frame`(`*tmp*`, value = list(c("A1", "A1.1", :
invalid 'dimnames' given for data frame
Any help is much appreciated.
Thank you.
--
View this message in context: http://r.789695.n4.nabble.com/Breaking-up-a-Row-in-R-transpose-tp4607658p4609658.html
Sent from the R help mailing list archive at Nabble.com.
On Fri, May 04, 2012 at 11:11:47AM -0700, marc212 wrote:
I have something like 28 rows and 6000 columns. How would I configure this with for loops.
The transformation may be splitted into simpler pieces using a for
loop over the 28 rows. Try the following.
orig <- rbind(
"0"=c(5, 6, 6, 7, 7, 9),
"1"=c(3, 4, 4, 3, 9, 9),
"2"=c(5, 2, 6, 4, 7, 4))
colnames(orig) <- rep(c("x", "y"), times=3)
out <- matrix(nrow=ncol(orig)/2, ncol=2*nrow(orig))
for (i in seq.int(length=nrow(orig))) {
out[, 2*(i-1) + 1:2] <- matrix(orig[i, ], nrow=nrow(out), ncol=2, byrow=TRUE)
}
out
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 5 6 3 4 5 2
[2,] 6 7 4 3 6 4
[3,] 7 9 9 9 7 4
Hope this helps.
Petr Savicky.
Hello, marc212 wrote
I am not opposed to for loops just do not know how to implement them.
I am sorry for all the questions I am trying to learn. The posted code
does not work for me in my set I am getting a :
Error in `dimnames<-.data.frame`(`*tmp*`, value = list(c("A1", "A1.1", :
invalid 'dimnames' given for data frame
Any help is much appreciated.
Thank you.
Try commenting out the line dimnames <- list(... etc ...) You could also use dput() to post an example of your data. (Small example.) Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Breaking-up-a-Row-in-R-transpose-tp4607658p4609748.html Sent from the R help mailing list archive at Nabble.com.