我有以下数据集:
structure(list(A = c(451.566484133459, 454.92869750881, 451.114252202941,
454.996314529852, 453.465382452704, 465.331269330636, 454.538567597461,
463.783172091384, 453.730115277698, 463.72438695636), b = c(-14.205718016514,
-14.2131358081788, -14.3200642093762, -14.5132647005539, -14.4081200637958,
-14.1349670121477, -14.507593447239, -14.1254822281233, -14.4903035105664,
-14.1928692151528), C = c(1469.46452712632, 1469.47892277398,
1469.69489134056, 1469.86771190593, 1470.21347499657, 1475.25989161757,
1475.12931967036, 1474.70874502286, 1474.14352261336, 1473.72350981991
), D = c(-10.6169715976949, -10.6421074258488, -10.6542208233633,
-10.6392491241976, -10.6712321191186, -10.6552464239492, -10.431683014845,
-10.7029234738311, -10.6610298766338, -10.607892938997), E = c(1025.04527283288,
1021.64525752271, 1017.15603173527, 1020.63728592133, 1015.14597799164,
1019.04943069105, 1019.71447994562, 1020.01958437602, 1013.27388793191,
1021.95151861996), F = c(-11.4656040580594, -11.1989433248374,
-11.7060300364731, -11.0700075928725, -11.5005713690914, -11.5228159566521,
-11.1757111501258, -11.2753143566779, -11.6646430517701, -11.5531222045509
)), .Names = c("A", "b", "C", "D", "E", "F"), row.names = c(NA,
10L), class = "data.frame")
数据框(df)如下所示:
A b C D E F
1 451.5665 -14.20572 1469.465 -10.61697 1025.045 -11.46560
2 454.9287 -14.21314 1469.479 -10.64211 1021.645 -11.19894
3 451.1143 -14.32006 1469.695 -10.65422 1017.156 -11.70603
4 454.9963 -14.51326 1469.868 -10.63925 1020.637 -11.07001
5 453.4654 -14.40812 1470.213 -10.67123 1015.146 -11.50057
6 465.3313 -14.13497 1475.260 -10.65525 1019.049 -11.52282
7 454.5386 -14.50759 1475.129 -10.43168 1019.714 -11.17571
8 463.7832 -14.12548 1474.709 -10.70292 1020.020 -11.27531
9 453.7301 -14.49030 1474.144 -10.66103 1013.274 -11.66464
10 463.7244 -14.19287 1473.724 -10.60789 1021.952 -11.55312
让我们说我需要剪切C
和D
列并将其从第11行开始粘贴到A
和B
。E
也是如此和F
。
我使用了rbind
,但它只转置了数据框,没有将C
和D
[1:10,]添加到A
和B
我的目标是获得以下内容:
A b
1 451.5665 -14.20572
2 454.9287 -14.21314
3 451.1143 -14.32006
4 454.9963 -14.51326
5 453.4654 -14.40812
6 465.3313 -14.13497
7 454.5386 -14.50759
8 463.7832 -14.12548
9 453.7301 -14.49030
10 463.7244 -14.19287
11 1469.465 -10.61697
12 1469.479 -10.64211
13 1469.695 -10.65422
. . .
. . .
. . .
. . .
30 . .
11-20的行代表df$C[1:10,]
和df$D[1:10,]
21-30行代表df$E[1:10,]
和df$F[1:10,]
答案 0 :(得分:3)
尝试:
这里的想法是创建一个逻辑索引来获取循环到整个数据集的备用列。
indx <- c(TRUE, FALSE)
head(df[indx],2) #it subsets the column `A`, `C`, and `E`
# A C E
#1 451.5665 1469.465 1025.045
#2 454.9287 1469.479 1021.645
通过df[!indx]
,您可以获得b
,D
和F
列。在此代码上使用unlist
会为您提供一个向量。所以,基本上,你得到两个vectors
,然后用于创建data.frame
。
df1 <- data.frame(A1=unlist(df[indx]), B1=unlist(df[!indx]))
row.names(df1) <- 1:nrow(df1)
df1
# A1 B1
#1 451.5665 -14.20572
#2 454.9287 -14.21314
#3 451.1143 -14.32006
#4 454.9963 -14.51326
#5 453.4654 -14.40812
#6 465.3313 -14.13497
#7 454.5386 -14.50759
#8 463.7832 -14.12548
#9 453.7301 -14.49030
#10 463.7244 -14.19287
#11 1469.4645 -10.61697
#12 1469.4789 -10.64211
#13 1469.6949 -10.65422
#14 1469.8677 -10.63925
#15 1470.2135 -10.67123
#16 1475.2599 -10.65525
#17 1475.1293 -10.43168
#18 1474.7087 -10.70292
#19 1474.1435 -10.66103
#20 1473.7235 -10.60789
#21 1025.0453 -11.46560
#22 1021.6453 -11.19894
#23 1017.1560 -11.70603
#24 1020.6373 -11.07001
#25 1015.1460 -11.50057
#26 1019.0494 -11.52282
#27 1019.7145 -11.17571
#28 1020.0196 -11.27531
#29 1013.2739 -11.66464
#30 1021.9515 -11.55312
或者您可以创建array
m1 <- as.matrix(df)
dim(m1) <- c(10,2,3)
do.call(`rbind`,lapply(seq_len(dim(m1)[3]), function(i) m1[,,i]))