views:

179

answers:

1

Is there a way in R to build a new dataset consisting of a given set of vectors -- median1, median2, median3, median4 -- which are median vectors from a previous dataset s?

median1 = apply(s[,c("A1","B1","C1","D1","E1","F1","G1","H1","I1")],1,median)
median2 = apply(s[,c("A2","B2","C2","D2","E2","F2","G2","H2","I2")],1,median)
median3 = apply(s[,c("A3","B3","C3","D3","E3","F3","G3","H3","I3")],1,median)
median4 = apply(s[,c("A4","B4","C4","D4","E4","F4","G4","H4","I4")],1,median)

plot(median1,median2, pch = ".")
+8  A: 

What do you mean "build a new dataset"? Like this?

s2 <- data.frame(median1, median2, median3, median4)

Or else use cbind:

s2 <- cbind(median1, median2, median3, median4)
Shane
Be careful the data.frame returns a data.frame object and the cbind returns a matrix objects, in this case it might not matter but in general the matrix requires the elements to all be of the same class whereas data.frame allows data in different columns to be of different classes.
Andrew Redd
That's a good point. In this case everything is numeric, so it won't make a difference (although the matrix will be memory efficient).
Shane