Hi,
I have yield data for several varieties and a randomly placed check (1 in every
8 column or "cols") in a field test arranged in a rows*cols grid
format (see image attached). Both "rows" & "cols" are
variables in the data set. I like to adjust "yield" variable for each
row listed as "variety" in variable "linecode" by dividing
its yield with the average yield of four nearest "check" (on the
rows*cols field grid) in variable "linecode". I like to have two
checks on the same row where one check is on the left and the other is on the
right side of a given variety. The other two checks should come from the two
neighboring columns ("cols"). If a check is missing on one or more
sides of a given variety, then I like to proceed with the calculation with only
the available checks around that given variety. If two checks on the
neighboring column are equidistance from a given variety then use position of
the variety to choose which one to use (If variety is in cols 1-8 then use check
from those cols; if variety is in cols 9-16 then use check from cols 9-16).
Below is the function I wrote which adjust yield values for each
"variety" (variable "linecode") by dividing its yield with
the average yield of all checks in the field. Instead of using average check
across the whole field, I like to use the four neighboring checks to make this
adjustment. I am struggling with specifying the four nearest checks in this
loop. I played around using "dist" function but without any success.
I tried searching for any packages that can do these nearest check adjustments
without any success. Any help will be appreciated.
-------------------function------------------------------------------
function (dataset, trait, control) {
m <- c()
x <- length(trait)
chkmean <- tapply(trait, control, mean, na.rm = T)
for (i in 1:x) {
m[i] <- ifelse(control[i] == "variety",
trait[i]/chkmean[1],
trait[i]/trait[i])
}
head(as.data.frame(m))
}
---------------------data----------------------------------------------------------------------
dput(dat)
structure(list(rows = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("1", "2",
"3",
"4"), class = "factor"), cols = structure(c(1L, 2L, 3L, 4L,
5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 16L, 15L,
14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L,
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 16L, 15L, 14L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L,
5L, 4L, 3L, 2L, 1L), .Label = c("1", "2", "3",
"4", "5", "6",
"7", "8", "9", "10", "11",
"12", "13", "14", "15", "16"),
class = "factor"),
plotid = c(289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L,
297L, 298L, 299L, 300L, 301L, 302L, 303L, 304L, 369L, 370L,
371L, 372L, 373L, 374L, 375L, 376L, 377L, 378L, 379L, 380L,
381L, 382L, 383L, 384L, 385L, 386L, 387L, 388L, 389L, 390L,
391L, 392L, 393L, 394L, 395L, 396L, 397L, 398L, 399L, 400L,
465L, 466L, 467L, 468L, 469L, 470L, 471L, 472L, 473L, 474L,
475L, 476L, 477L, 478L, 479L, 480L), yield = c(5.1, 5.5,
5, 5.5, 6.2, 5.1, 5.5, 5.2, 5, 5, 3.9, 4.6, 5, 4.4, 5.1,
4.3, 4.4, 4.2, 3.9, 4.6, 4.8, 5.4, 4.7, 5.5, 5.3, 4.8, 5.8,
4.6, 5.8, 5.5, 5.3, 5.6, 5.6, 5, 4.8, 4.9, 5.2, 5.3, 4.6,
4.8, 5.3, 4.2, 4.6, 4.2, 4.2, 4, 3.9, 4.5, 5.4, 4.8, 4.6,
5.2, 4.9, 5.1, 4.5, 5.8, 5.2, 4.7, 4.8, 5.3, 5.8, 4.9, 5.9,
4.5), line = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 1L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L,
20L, 1L, 21L, 22L, 1L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 32L, 33L, 1L, 34L, 35L, 36L, 37L, 38L, 39L, 40L,
41L, 42L, 1L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 1L,
51L, 52L, 53L, 54L, 1L, 55L, 56L, 57L), .Label = c("CHK",
"V002", "V003", "V004", "V005",
"V006", "V007", "V008", "V009",
"V010", "V011", "V012", "V013",
"V014", "V015", "V016", "V017",
"V018", "V019", "V020", "V021",
"V022", "V023", "V024", "V025",
"V026", "V027", "V028", "V029",
"V030", "V031", "V032", "V033",
"V034", "V035", "V036", "V037",
"V038", "V039", "V040", "V041",
"V042", "V043", "V044", "V045",
"V046", "V047", "V048", "V049",
"V050", "V051", "V052", "V053",
"V054", "V055", "V056", "V057"
), class = "factor"), linecode = structure(c(1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L), .Label = c("check", "variety"), class =
"factor")), .Names = c("rows",
"cols", "plotid", "yield", "line",
"linecode"), row.names = c(NA,
-64L), class = "data.frame")
-------------------------------------------------------------------------------------------------
My expected output is in column "adj_yield" below:
rows cols plotid yield line linecode adj_yield
1 1 1 289 5.1 CHK check check
2 1 2 290 5.5 V002 variety 1.071
3 1 3 291 5.0 V003 variety 0.974
4 1 4 292 5.5 V004 variety 1.071
5 1 5 293 6.2 V005 variety 1.208
6 1 6 294 5.1 V006 variety 0.994
7 1 7 295 5.5 V007 variety 1.071
8 1 8 296 5.2 V008 variety 1.013
9 1 9 297 5.0 V009 variety 0.974
10 1 10 298 5.0 CHK check check
11 1 11 299 3.9 V010 variety 0.750
12 1 12 300 4.6 V011 variety 0.885
13 1 13 301 5.0 V012 variety 0.962
14 1 14 302 4.4 V013 variety 0.846
15 1 15 303 5.1 V014 variety 0.981
16 1 16 304 4.3 V015 variety 0.827
17 2 16 369 4.4 V016 variety check
18 2 15 370 4.2 V017 variety 0.881
19 2 14 371 3.9 V018 variety 0.818
20 2 13 372 4.6 V019 variety 0.965
21 2 12 373 4.8 V020 variety 1.007
22 2 11 374 5.4 CHK check check
23 2 10 375 4.7 V021 variety 0.959
24 2 9 376 5.5 V022 variety 1.053
25 2 8 377 5.3 CHK check check
26 2 7 378 4.8 V023 variety 0.923
27 2 6 379 5.8 V024 variety 1.115
28 2 5 380 4.6 V025 variety 0.885
29 2 4 381 5.8 V026 variety 1.115
30 2 3 382 5.5 V027 variety 1.058
31 2 2 383 5.3 V028 variety 1.019
32 2 1 384 5.6 V029 variety 1.077
-----------------session
info------------------------------------------------------------------------
R version 3.2.1 (2015-06-18)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rlist_0.4.5.1 mapplots_1.5 agridat_1.12
loaded via a namespace (and not attached):
[1] magrittr_1.5 plyr_1.8.3 tools_3.2.1 reshape2_1.4.1
Rcpp_0.12.0 stringi_0.5-5
[7] grid_3.2.1 data.table_1.9.4 stringr_1.0.0 chron_2.3-47
lattice_0.20-31
Nilesh Dighe
(806)-252-7492 (Cell)
(806)-741-2019 (Office)
This e-mail message may contain privileged and/or confidential information, and
is intended to be received only by persons entitled
to receive such information. If you have received this e-mail in error, please
notify the sender immediately. Please delete it and
all attachments from any servers, hard drives or any other media. Other use of
this e-mail by you is strictly prohibited.
All e-mails and attachments sent and received are subject to monitoring, reading
and archival by Monsanto, including its
subsidiaries. The recipient of this e-mail is solely responsible for checking
for the presence of "Viruses" or other "Malware".
Monsanto, along with its subsidiaries, accepts no liability for any damage
caused by any such code transmitted by or accompanying
this e-mail or any attachment.
The information contained in this email may be subject to the export control
laws and regulations of the United States, potentially
including but not limited to the Export Administration Regulations (EAR) and
sanctions regulations issued by the U.S. Department of
Treasury, Office of Foreign Asset Controls (OFAC). As a recipient of this
information you are obligated to comply with all
applicable U.S. export laws and regulations.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fieldlayout.png
Type: image/png
Size: 11941 bytes
Desc: fieldlayout.png
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20151022/400b8ab1/attachment.png>