我有一个由 3D 点坐标组成的 data.frame。4 个可能的点 A1、A2、A3、A4 有 4 x 3 列。在每一行中,恰好有两个非零点(A1...A4 中的任意两个)。我想知道是否可以在不使用循环的情况下将非零三元组提取到新的 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")
谢谢