我有一个像这样的数据框
df<- structure(list(s = structure(1:5, .Label = c("aa", "bb", "cc",
"dd", "ee"), class = "factor"), x = c("-0.236105995167248,-1.18643591933632,1.58980931618443,-0.331783574898037,-0.651731871006788,0.407805765584701",
"-0.127273667956423,1.18952542054957,-1.58950574965751,-0.304711433481107,1.40400458577633,1.14109106786785",
"-0.843636178194365,1.96020358181084,0.250099677119141,-0.168523273691266,-1.20578057039011,-1.21812075553223",
"1.38888748045579,-0.366027289095327,-0.0943004596886008,0.264566505263093,-0.315415210135534,-0.0654472787707433",
"1.98055470864296,-0.120256948050693,0.863815283624947,0.302448953974203,-0.0108923675427342,0.268615537295431"
)), class = "data.frame", row.names = c(NA, -5L))
我想在 s 列和 x 列之间添加df2
df2 <- structure(list(mynew = structure(1L, .Label = "5.470864296,7.120256948050693,1.947,0.302448953974203,-25,150", class = "factor")), row.names = c(NA,
-1L), class = "data.frame")
期望输出是这样
drout<- structure(list(s = structure(1:5, .Label = c("aa", "bb", "cc",
"dd", "ee"), class = "factor"), mynew = structure(c(1L, 1L, 1L,
1L, 1L), .Label = "5.470864296,7.120256948050693,1.947,0.302448953974203,-25,150", class = "factor"),
x = structure(c(2L, 1L, 3L, 4L, 5L), .Label = c("-0.127273667956423,1.18952542054957,-1.58950574965751,-0.304711433481107,1.40400458577633,1.14109106786785",
"-0.236105995167248,-1.18643591933632,1.58980931618443,-0.331783574898037,-0.651731871006788,0.407805765584701",
"-0.843636178194365,1.96020358181084,0.250099677119141,-0.168523273691266,-1.20578057039011,-1.21812075553223",
"1.38888748045579,-0.366027289095327,-0.0943004596886008,0.264566505263093,-0.315415210135534,-0.0654472787707433",
"1.98055470864296,-0.120256948050693,0.863815283624947,0.302448953974203,-0.0108923675427342,0.268615537295431"
), class = "factor")), class = "data.frame", row.names = c(NA,
-5L))
答案 0 :(得分:1)
我们可以使用cbind
并对列进行重新排序
out <- cbind(df, df2)[c("s", "mynew", "x")]
out
# s mynew x
#1 aa 5.470864296,7.120256948050693,1.947,0.302448953974203,-25,150 -0.236105995167248,-1.18643591933632,1.58980931618443,-0.331783574898037,-0.651731871006788,0.407805765584701
#2 bb 5.470864296,7.120256948050693,1.947,0.302448953974203,-25,150 -0.127273667956423,1.18952542054957,-1.58950574965751,-0.304711433481107,1.40400458577633,1.14109106786785
#3 cc 5.470864296,7.120256948050693,1.947,0.302448953974203,-25,150 -0.843636178194365,1.96020358181084,0.250099677119141,-0.168523273691266,-1.20578057039011,-1.21812075553223
#4 dd 5.470864296,7.120256948050693,1.947,0.302448953974203,-25,150 1.38888748045579,-0.366027289095327,-0.0943004596886008,0.264566505263093,-0.315415210135534,-0.0654472787707433
#5 ee 5.470864296,7.120256948050693,1.947,0.302448953974203,-25,150 1.98055470864296,-0.120256948050693,0.863815283624947,0.302448953974203,-0.0108923675427342,0.268615537295431