r - add values to a dataframe every nth row or corresponding to a spec. column value -
r-noob here question not solve himself
i hava data frame length of 250
head(abto, 20) see transekt plant blatt# breiteo breitez bez 1: abt 1 1 2.0182 5.3980 1 2: abt 1 1 1.9730 4.2522 1 3: abt 1 1 1.8024 3.7587 1 4: abt 1 2 2.2081 4.2880 2 5: abt 1 2 2.2858 6.1115 2 6: abt 1 2 1.8532 5.7426 2 7: abt 1 3 2.0384 4.9074 2 8: abt 1 3 2.0757 4.8801 2 9: abt 1 3 1.8034 4.6111 2 10: abt 1 4 1.9567 4.8879 2 11: abt 1 4 1.9080 5.0652 2 12: abt 1 4 1.8346 4.8862 2 13: abt 1 5 2.0282 4.5545 1 14: abt 1 5 2.1356 5.7157 1 15: abt 1 5 1.7594 6.1688 1 16: abt 2 1 1.6457 5.2868 1 17: abt 2 1 1.6942 5.0414 1 18: abt 2 1 2.0544 5.6711 1 19: abt 2 2 2.1342 5.2867 2 20: abt 2 2 1.9107 6.2139 2
and vector length of 84 (only third minus 2 first one)
> head(databtzl) abtl.wert 1 22.7738 2 24.9137 3 24.9474 4 25.0498 5 25.0431 6 21.1024
basically have dataframe 3 measurements of width per leaf(blatt#) , vector 1 measurment of length. trying merge them in one, putting length values on every 3rd row.
i guess there many solutions, thinking of creating 2 blank cells in between every value of vector , merge them.
since there iregularities in data frame (twice 2 values width instead of 3)i thinking of function/loop assign value of vector everytime value of abto$blatt# changing.
sorry cumbersome question, hope understands problem.
thanks in advance!
edit: expected output following table abtl.wert values databtzl vector...
see transekt plant blatt# breiteo breitez bez length 1: abt 1 1 2.0182 5.3980 1 abtl.wert1 2: abt 1 1 1.9730 4.2522 1 3: abt 1 1 1.8024 3.7587 1 4: abt 1 2 2.2081 4.2880 2 abtl.wert2 5: abt 1 2 2.2858 6.1115 2 6: abt 1 2 1.8532 5.7426 2 7: abt 1 3 2.0384 4.9074 2 abtl.wert3 8: abt 1 3 2.0757 4.8801 2 9: abt 1 3 1.8034 4.6111 2
if understand problem, following should work:
## compute rows in abto add values databtz$abtl.wert ## here, changes in adjoining rows of blatt# column ## using diff. descending in rows, row before change have ## diff != 0. want mark next row first row, ## prepend resulting vector true rind <- c(true, diff(abto$`blatt#`) != 0) ## matter of adding new column abto named length ## , inserting values databtz1$abtl.wert rows ## marked (identified which(rind==1)) result <- data.frame(abto, length=rep(na,nrow(abto))) result[which(rind==true),"length"] <- databtz1$abtl.wert
this answer assumes number of rows in databtz1
matches number of rows @ abto$blatt#
changes.
with input data:
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))
i this:
print(result) ## see transekt plant blatt. breiteo breitez bez length ##1 abt 1 1 2.0182 5.3980 1 22.7738 ##2 abt 1 1 1.9730 4.2522 1 na ##3 abt 1 1 1.8024 3.7587 1 na ##4 abt 1 2 2.2081 4.2880 2 24.9137 ##5 abt 1 2 2.2858 6.1115 2 na ##6 abt 1 2 1.8532 5.7426 2 na ##7 abt 1 3 2.0384 4.9074 2 24.9474 ##8 abt 1 3 2.0757 4.8801 2 na ##9 abt 1 4 1.9567 4.8879 2 25.0498 ##10 abt 1 4 1.9080 5.0652 2 na ##11 abt 1 4 1.8346 4.8862 2 na ##12 abt 1 5 2.0282 4.5545 1 25.0431 ##13 abt 1 5 2.1356 5.7157 1 na ##14 abt 1 5 1.7594 6.1688 1 na ##15 abt 2 1 1.6457 5.2868 1 21.1024 ##16 abt 2 1 1.6942 5.0414 1 na ##17 abt 2 1 2.0544 5.6711 1 na ##18 abt 2 2 2.1342 5.2867 2 28.9083 ##19 abt 2 2 1.9107 6.2139 2 na
note removed row blatt#
equal 3
original posted data simulate irregularities, , added row in databtz1$abtl.wert
have equal number of changes in blatt#
rows of databtz1$abtl.wert
.
Comments
Post a Comment