r-noob这里有一个不能自己解决的问题
我有一个长度为250的数据框
head(abto, 20)
See Transekt plant Blatt# breiteo breitez bez
1: ABT A 1 1 2.0182 5.3980 1
2: ABT A 1 1 1.9730 4.2522 1
3: ABT A 1 1 1.8024 3.7587 1
4: ABT A 1 2 2.2081 4.2880 2
5: ABT A 1 2 2.2858 6.1115 2
6: ABT A 1 2 1.8532 5.7426 2
7: ABT A 1 3 2.0384 4.9074 2
8: ABT A 1 3 2.0757 4.8801 2
9: ABT A 1 3 1.8034 4.6111 2
10: ABT A 1 4 1.9567 4.8879 2
11: ABT A 1 4 1.9080 5.0652 2
12: ABT A 1 4 1.8346 4.8862 2
13: ABT A 1 5 2.0282 4.5545 1
14: ABT A 1 5 2.1356 5.7157 1
15: ABT A 1 5 1.7594 6.1688 1
16: ABT A 2 1 1.6457 5.2868 1
17: ABT A 2 1 1.6942 5.0414 1
18: ABT A 2 1 2.0544 5.6711 1
19: ABT A 2 2 2.1342 5.2867 2
20: ABT A 2 2 1.9107 6.2139 2
和一个长度为84的向量(只有第三个减去2作为第一个)
> head(databtzl)
abtl.wert
1 22.7738
2 24.9137
3 24.9474
4 25.0498
5 25.0431
6 21.1024
基本上我有一个数据帧,每个叶子有3个宽度测量值(Blatt#)和一个测量长度的矢量。我试图将它们合并为一个,但是将长度值放在每第3行。
我想有很多解决方案,我想在向量的每个值之间创建2个空白单元格然后合并它们。
由于数据帧中存在一些不规则性(宽度的两次只有2个而不是3个),每当abto $ Blatt#的值发生变化时,我就会想到一个函数/循环来分配向量的值。
抱歉这个麻烦的问题,我希望有人理解我的问题。
提前致谢!
编辑:我的预期输出是下表,abtl.wert是databtzl向量中的值......
See Transekt plant Blatt# breiteo breitez bez length
1: ABT A 1 1 2.0182 5.3980 1 abtl.wert1
2: ABT A 1 1 1.9730 4.2522 1
3: ABT A 1 1 1.8024 3.7587 1
4: ABT A 1 2 2.2081 4.2880 2 abtl.wert2
5: ABT A 1 2 2.2858 6.1115 2
6: ABT A 1 2 1.8532 5.7426 2
7: ABT A 1 3 2.0384 4.9074 2 abtl.wert3
8: ABT A 1 3 2.0757 4.8801 2
9: ABT A 1 3 1.8034 4.6111 2
答案 0 :(得分:0)
如果我理解您的问题,以下内容应该有效:
## Compute the rows in abto to add the values from databtz$abtl.wert
## Here, we look for changes in adjoining rows of the Blatt# column
## using diff. Descending in rows, the row before the change will have
## a diff != 0. We want to mark the next row as well as the first row,
## so prepend this resulting vector with TRUE
rind <- c(TRUE, diff(abto$`Blatt#`) != 0)
## Then it is just a matter of adding a new column to abto named length
## and inserting the values from databtz1$abtl.wert only for the rows
## that are marked (identified by which(rind==1))
result <- data.frame(abto, length=rep(NA,nrow(abto)))
result[which(rind==TRUE),"length"] <- databtz1$abtl.wert
此答案假设databtz1
中的行数与abto$Blatt#
更改的行数相匹配。
使用此输入数据:
abto <- structure(list(See = c("ABT", "ABT", "ABT", "ABT", "ABT", "ABT",
"ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT", "ABT",
"ABT", "ABT", "ABT", "ABT"), Transekt = c("A", "A", "A", "A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A"), plant = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), `Blatt#` = c(1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 2L, 2L
), breiteo = c(2.0182, 1.973, 1.8024, 2.2081, 2.2858, 1.8532,
2.0384, 2.0757, 1.9567, 1.908, 1.8346, 2.0282, 2.1356, 1.7594,
1.6457, 1.6942, 2.0544, 2.1342, 1.9107), breitez = c(5.398, 4.2522,
3.7587, 4.288, 6.1115, 5.7426, 4.9074, 4.8801, 4.8879, 5.0652,
4.8862, 4.5545, 5.7157, 6.1688, 5.2868, 5.0414, 5.6711, 5.2867,
6.2139), bez = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L)), .Names = c("See", "Transekt",
"plant", "Blatt#", "breiteo", "breitez", "bez"), class = "data.frame", row.names = c(NA,
-19L))
databtz1 <- structure(list(abtl.wert = c(22.7738, 24.9137, 24.9474, 25.0498,
25.0431, 21.1024, 28.9083)), .Names = "abtl.wert", class = "data.frame", row.names = c(NA,
-7L))
我明白了:
print(result)
## See Transekt plant Blatt. breiteo breitez bez length
##1 ABT A 1 1 2.0182 5.3980 1 22.7738
##2 ABT A 1 1 1.9730 4.2522 1 NA
##3 ABT A 1 1 1.8024 3.7587 1 NA
##4 ABT A 1 2 2.2081 4.2880 2 24.9137
##5 ABT A 1 2 2.2858 6.1115 2 NA
##6 ABT A 1 2 1.8532 5.7426 2 NA
##7 ABT A 1 3 2.0384 4.9074 2 24.9474
##8 ABT A 1 3 2.0757 4.8801 2 NA
##9 ABT A 1 4 1.9567 4.8879 2 25.0498
##10 ABT A 1 4 1.9080 5.0652 2 NA
##11 ABT A 1 4 1.8346 4.8862 2 NA
##12 ABT A 1 5 2.0282 4.5545 1 25.0431
##13 ABT A 1 5 2.1356 5.7157 1 NA
##14 ABT A 1 5 1.7594 6.1688 1 NA
##15 ABT A 2 1 1.6457 5.2868 1 21.1024
##16 ABT A 2 1 1.6942 5.0414 1 NA
##17 ABT A 2 1 2.0544 5.6711 1 NA
##18 ABT A 2 2 2.1342 5.2867 2 28.9083
##19 ABT A 2 2 1.9107 6.2139 2 NA
请注意,我从原始发布的数据中删除了Blatt#
等于3
的行,以模拟您的违规行为,并在databtz1$abtl.wert
中添加了一行,以便我们在Blatt#
databtz1$abtl.wert
行Cells()
。