次のようなデータフレームがあります。
TRUE x_value1 x_value2 y_value1 y_value2 x_id y_id
0 1 4 11 14 1 7
1 2 5 12 15 2 8
0 3 6 13 16 3 9
そして、このデータ フレームに行を追加し、x と y を切り替えて、行 4 で x_value1=y_value1 および x_id=y_id および y_value1=x_value1... などとします。
このような:
TRUE x_value1 x_value2 y_value1 y_value2 x_id y_id
0 1 4 11 14 1 7
1 2 5 12 15 2 8
0 3 6 13 16 3 9
0 11 14 1 4 7 1
1 12 15 2 5 8 2
0 13 16 3 6 9 3
forループでこれを行うことができますが、それには時間がかかります
例えば
for (i in 1:3)
{ dataframe[i+3,2]<-dataframe[i,4] //for i=1, finds 4th row, column "x_value1" and switches it with first row, column "y_value1"
dataframe[i+3,3]<-dataframe[i,5]
...etc.
}
私が持っているデータ フレームの例 (上記の最初の表):
structure(list(TRUE. = c(0L, 1L, 0L), x_value1 = 1:3, x_value2 = 4:6,
y_value1 = 11:13, y_value2 = 14:16, x_id = 1:3, y_id = 7:9), .Names = c("TRUE.",
"x_value1", "x_value2", "y_value1", "y_value2", "x_id", "y_id"
), row.names = c(NA, 3L), class = "data.frame")
望ましい (上の 2 番目の表):
structure(list(TRUE. = c(0L, 1L, 0L), x_value1 = 1:3, x_value2 = 4:6,
y_value1 = 11:13, y_value2 = 14:16, x_id = 1:3, y_id = 7:9), .Names = c("TRUE.",
"x_value1", "x_value2", "y_value1", "y_value2", "x_id", "y_id"
), row.names = c(NA, 3L), class = "data.frame")