我的数据框看起来像这样:
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循环执行此操作,但这需要很长时间
e.g。
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")
期望(上面的第二个表格):
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")
答案 0 :(得分:6)
如果dd是您的原始数据框,那么您可以尝试:
dd2 <- cbind(dd[1], dd[4:5], dd[2:3],dd[7:6])
names(dd2) <- names(dd)
dd <- rbind(dd, dd2)
答案 1 :(得分:5)
更多地以“编程方式”执行此操作:使用带有列名称模式的grep:
> grep("y_", names(dat))
[1] 4 5 7
> grep("x_", names(dat))
[1] 2 3 6
dat2 <- dat
# Replace all the dat2 "y_"-names with "x_"-names
colnames(dat2)[grep("y_", colnames(dat))] <-
colnames(dat)[grep("x_", colnames(dat))]
# Replace alldat2 the "x_"-names with "y_"-names
colnames(dat2)[grep("x_", colnames(dat))] <-
colnames(dat)[grep("y_", colnames(dat))]
colnames(dat2)
#[1] "TRUE." "y_value1" "y_value2" "x_value1" "x_value2" "y_id"
#[7] "x_id"
rbind(dat,dat2)
#-----------------
TRUE. x_value1 x_value2 y_value1 y_value2 x_id y_id
1 0 1 4 11 14 1 7
2 1 2 5 12 15 2 8
3 0 3 6 13 16 3 9
4 0 11 14 1 4 7 1
5 1 12 15 2 5 8 2
6 0 13 16 3 6 9 3
答案 2 :(得分:2)
您可以只重命名列,然后让rbind负责匹配订单,而不是像其他人建议的那样重新排列数据框:
dat2 <- dat
colnames (dat2) <- colnames (dat) [c (1, 4, 5, 2, 3, 7, 6)]
dat3 <- rbind (dat, dat2)
编辑:或者更多地以编程方式,以避免必须输入列索引,我一直做错了
dat2 <- dat
newnames <- gsub ("(x|y)_", "\\1\\1_", colnames(dat))
newnames <- gsub ("xx_", "y_", newnames)
newnames <- gsub ("yy_", "x_", newnames)
colnames(dat2) <- newnames
dat3 <- rbind (dat, dat2)