6

m概要: データ フレームに行を追加するにはどうすればよいですかm X n。新しい行はそれぞれ既存の行の後に挿入されますか? 基本的に既存の行をコピーしますが、1 つの変数を変更します。

詳細:別の質問を参照して、rglのsegments3d関数でやりたいことができると思います。x、y、z ポイントのセットがありますが、これらは線分のセットの 1 つの終点にすぎません。もう一方の終点は、4 番目の変数 X、Y、Z、Z_Length として指定された Z 次元で非常に数メートル離れています。私の用語では、東向き、北向き、標高、長さです。

rglのドキュメントによると、「ポイントはsegments3dによってペアで取得されます」。したがって、データ フレームを変更して、2 行ごとに Z 変数を変更して (Z から Z_Length を差し引いて) 余分なエントリを含める必要があると思います。視覚的には、次のようにする必要があります。

+-------+---------+----------+-----------+---------+
| Label | easting | northing | elevation | length  |
+-------+---------+----------+-----------+---------+
| 47063 |  554952 |  5804714 | 32.68     | 619.25  |
| 47311 |  492126 |  5730703 | 10.40     | 1773.00 |
+-------+---------+----------+-----------+---------+

これに:

+-------+---------+----------+-----------+---------+
| Label | easting | northing | elevation | length  |
+-------+---------+----------+-----------+---------+
| 47063 |  554952 |  5804714 | 32.68     | 619.25  |
| 47063 |  554952 |  5804714 | -586.57   | 619.25  |
| 47311 |  492126 |  5730703 | 10.40     | 1773.00 |
| 47311 |  492126 |  5730703 | -1762.26  | 1773.00 |
+-------+---------+----------+-----------+---------+

リンクされた質問のデータサンプルが利用可能です。

4

5 に答える 5

2

あなたが何をしているのかを理解していれば、可能なアプローチの1つを次に示します。

dat <- head(CO2, 10) # fake data set

L1 <- lapply(1:nrow(dat), function(i) {
    dat2x <-  dat[i, ]
    dat2x[4] <- dat[i, 4] - dat[i, 5]
    rbind(dat[i, ], dat2x)
})
do.call(rbind, L1)

編集: e4e5f4 の優れた応答に完全に取り組んでいます:

new <- dat[rep(1:nrow(dat),1,each=2),]
new[c(F, T),4] <- dat[4] - dat[5]

どちらも同等ですが、alter の方がはるかに速いと思います。

于 2013-05-09T02:56:13.727 に答える
2

「e4e5f4's」応答から変更

行の間に空白行を挿入する

    # sample matrix of df 
    old <-matrix(1:9, ncol=3)

    # double all rows 
    new <- old[rep(1:nrow(old),1,each=2),]

    # replace all duplicates with blank cells
    new[c(seq(2, dim(new)[1], by=2)), ] <- ""

    old # original 
    new # all ok ;)
于 2015-04-19T16:09:41.980 に答える
0

行数が 2 倍の新しいマトリックスを作成し、古いデータ フレーム要素を新しいマトリックスの適切な位置に戻し、ギャップを残すことができます。標高の計算を実行し、新しいマトリックスを作成してから、調整された標高マトリックスをより大きな新しいマトリックスに挿入します。次に、行列をデータ フレームに変換します。

test <- matrix(1:9, ncol=3)
ind <- (1:nrow(test)*2 - 1 # - 1 b/c you want to insert rows after, not before, existing rows
test_new <- matrix(rep(NA, (nrow(test)*2*ncol(test))), ncol=ncol(test))
test_new[ind,] <- test

test_elev <- test #create a new matrix that will have adjusted elevations
test_elev[,2] <- test[,2] - test[,3] #e.g., test[,2] is the elevation column, and test[,3] is the length column
test_new[ind+1,] <- test_elev #then put the new elevations into the new matrix

#if you need it to be a data.frame() again, you can use `as.data.frame(test_new)`
于 2013-05-09T02:57:59.897 に答える