2
我有一個由3D中點的座標組成的data.frame。對於4個可能的點A1,A2,A3,A4,有4 x 3列。在每一行中,都有兩個非零點(A1 ... A4中的任意兩個)。我想知道是否可以在不使用循環的情況下將非零三元組提取到新的data.frame中。從data.frame中提取子集
原始data.frame:
df<-data.frame(
rbind(
c(0,0,0,1,2,3,0,0,0,5,4,1),
c(1,2,2,1,2,3,0,0,0,0,0,0),
c(0,0,0,0,0,0,2,1,1,5,4,1),
c(0,0,0,1,3,1,2,1,1,0,0,0)
))
colnames(df) <- c("x1","y1","z1","x2","y2","z2","x3","y3","z3","x4","y4","z4")
我想要什麼:
df2<-data.frame(
rbind(
c(1,2,3,5,4,1),
c(1,2,2,1,2,3),
c(2,1,1,5,4,1),
c(1,3,1,1,2,3)
))
colnames(df2) <- c("x5","y5","z5","x6","y6","z6")
感謝
當你的意思是兩個非零分,可他們是(5,4,0)?或者他們將永遠不會是零?例如:這可能發生嗎? 'c(0,0,0,1,2,3,0,0,5,4,0)'? – Arun 2013-04-23 10:11:22
他們永遠不會有零組件 – mira 2013-04-23 10:39:22