Yolande Tra
2012-Jul-18 13:47 UTC
[R] loop searching the id corresponding to the given index (timestamp)
Hello, I have the following loop for two data sets: diveData_2008 and diveData_2009. It uses two other data: diveCond_all and fishTable. The problem is at the point to identify the dive_id for the given index (index is timestamp). It keeps on saying for the1st loop Error in fishReport$dive_id[i] <- dive_id : replacement has length zero for the 2nd loop Error in fishReport$dive_id[i + j] <- dive_id : replacement has length zero That is where the only problem resides. Here is the code and below are the first 15 lines of data N <- dim(diveData_2009)[1] + dim(diveData_2008)[1] fishReport <- data.frame(matrix(0, nrow=N, ncol=11)) names(fishReport) <- c("dive_id", "site", "section", "level", "transect", "depth", "species", "count", "size") for(i in 1:dim(diveData_2008)[1]){ # Which dive is this observation from thisIndex <- as.character(index(diveData_2008[i,])) dive_id <- diveCond_all$dive_id[diveCond_all$timestamp==thisIndex] cat(dive_id, thisIndex, "\n") # Store the pertinent data in the fish report data.frame fishReport$dive_id[i] <- dive_id fishReport$site[i] <- diveData_2008[i,"site"] fishReport$level[i] <- diveData_2008[i,"level"] fishReport$section[i] <- 0 fishReport$transect[i] <- as.numeric(diveData_2008[i,"TRANSECT"]) fishReport$depth[i] <- as.numeric(diveData_2008[i,"depth"]) fishReport$species[i] <- fishTable$name_2009[fishTable$name_2008==as.character(diveData_2008[i,"species"]) ][1] fishReport$count[i] <- as.numeric(diveData_2008[i,"count"]) fishReport$size[i] <- as.numeric(diveData_2008[i,"size"]) fishReport$visibility[i] <- as.numeric(diveData_2008[i,"VIS_M"]) fishReport$swell[i] <-as.numeric(diveData_2008[i,"swell_URSKI"]) } j<-i ## The 2009 dives for(i in i+1:dim(diveData_2009)[1]){ thisIndex <- as.character(index(diveData_2009[i,])) dive_id <- diveCond_all$dive_id[diveCond_all$timestamp==thisIndex] cat(dive_id, thisIndex, "\n") fishReport$dive_id[i+j] <- dive_id fishReport$site[i+j] <- "Hopkins" fishReport$level[i+j] <- diveData_2009[i,"level"] fishReport$transect[i+j] <- as.numeric(diveData_2009[i,"TRANSECT"]) fishReport$depth[i+j] <- as.numeric(diveData_2009[i,"depth"]) fishReport$species[i+j] <- diveData_2009[i,"species"] fishReport$count[i+j] <- as.numeric(diveData_2008[i,"count"]) fishReport$size[i+j] <- as.numeric(diveData_2009[i,"size"]) fishReport$visibility[i+j] <- as.numeric(diveData_2008[i,"VIS_M"]) } diveData_2008 (first 15 lines) structure(c(" 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 2", " 2", " 2", " 2", " 2", " 2", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8:49", "8:49", "8:49", "8:49", "8:49", "8:49", "8:49", "8:49", "8:49", "10:03", "10:03", "10:03", "10:03", "10:03", "10:03", "S. OYT", "S. atrovirens", "S. atrovirens", "S. atrovirens", "O. pictus", "C. nicholsii", "C. nicholsii", "D. vacca", "No organisms", "S. OYT", "S. atrovirens", "S. atrovirens", "S. atrovirens", "S. chrysomelas", "O. pictus", " 15", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 6", "23", "27", "29", " 8", " 8", " 9", "18", NA, " 6", "23", "28", "35", "32", " 8", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "15", "15", "15", "15", "15", "15", "15", "15", "15", "10", "10", "10", "10", "10", "10", "B", "B", "B", "B", "B", "B", "B", "B", "M", "B", "B", "B", "B", "B", "B", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 2", " 2", " 2", " 2", " 2", " 2", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "1.0686708", "1.0686708", "1.0686708", "1.0686708", "1.0686708", "1.0686708", "1.0686708", "1.0686708", "1.0686708", "1.1111950", "1.1111950", "1.1111950", "1.1111950", "1.1111950", "1.1111950"), .indexCLASS c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", class = c("xts", "zoo"), index = structure(c(1218026940, 1218026940, 1218026940, 1218026940, 1218026940, 1218026940, 1218026940, 1218026940, 1218026940, 1218031380, 1218031380, 1218031380, 1218031380, 1218031380, 1218031380 ), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(15L, 13L), .Dimnames = list(NULL, c("dive_id", "date", "time", "species", "count", "size", "site", "depth", "level", "TRANSECT", "VIS_M", "TEMP_C", "swell_URSKI"))) diveData_2009 (first 15 lines) structure(c("62", "62", "62", "62", "62", "62", "62", "62", "62", "62", "62", "62", "62", "62", "62", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "E. lateralis", "E. lateralis", "E. lateralis", "E. lateralis", "O. pictus", "S. OYT", "S. OYT", "S. atrovirens", "S. atrovirens", "S. atrovirens", "S. atrovirens", "S. carnatus", "S. carnatus", "S. carnatus", "S. carnatus", " 2", " 1", " 1", " 1", " 1", " 3", " 1", " 3", " 2", " 1", " 1", " 1", " 2", " 1", " 1", "15", "22", "25", "27", " 4", " 6", " 8", " 5", "20", "23", "25", " 5", "20", "26", "30", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "15", "15", "15", "15", "15", "15", "15", "15", "15", "15", "15", "15", "15", "15", "15", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000"), class = c("xts", "zoo"), .indexCLASS c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", index = structure(c(1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840), tzone = "", tclass = c("POSIXct", "POSIXt" )), .Dim = c(15L, 11L), .Dimnames = list(NULL, c("dive_id", "date", "time", "species", "count", "size", "site", "depth", "level", "TRANSECT", "VIS_M"))) diveCond_all (first 15 lines) structure(list(dive_id = structure(1:15, .Label = c(" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97"), class = "factor"), timestamp = structure(c(1218026940, 1218031380, 1218032700, 1218202920, 1218207180, 1218213720, 1218478320, 1218478320, 1218463800, 1218463800, 1218466860, 1218467760, 1218634260, 1218638100, 1218639720), class = c("POSIXct", "POSIXt"), tzone = ""), visability = structure(c(5L, 5L, NA, 6L, NA, NA, NA, NA, NA, NA, 11L, 10L, NA, NA, NA), .Label = c(" 1.5", " 2.0", " 2.5", " 3.0", " 3.5", " 4.0", " 5.0", " 5.5", " 6.0", " 6.5", " 7.0", " 7.5", " 8.0", " 9.0", "10.0", "12.0", "13.0", "3.0000", "4.0000", "5.0000", "5.4864", "6.0000", "6.0960"), class = "factor"), r_wvht = c(0.91, 1, 1.02, 0.76, 0.78, 0.78, 1.35, 1.35, 1.57, 1.57, 1.53, 1.53, 1.51, 1.53, 1.69), r_dpd = c(14.29, 4.35, 4.35, 13.33, 13.33, 8.33, 8.33, 8.33, 8.33, 8.33, 8.33, 8.33, 9.09, 9.09, 8.33), r_apt = c(4.59, 4.45, 4.26, 5.87, 6.27, 6.58, 6.7, 6.7, 6.67, 6.67, 6.66, 6.5, 6.99, 6.95, 7.19), r_mwd = c(229, 291, 297, 226, 228, 260, 287, 287, 291, 291, 290, 290, 294, 292, 298), r_wtmp = c(12.6, 12.6, 12.5, 12.7, 12.6, 12.6, 12.8, 12.8, 12.7, 12.7, 12.7, 12.7, 13.4, 13.4, 13.5), l_salinity = c("33.513", "33.513", "33.513", "33.513", "33.513", "33.513", "33.513", "33.513", "33.513", "33.513", "33.513", "33.513", "33.513", "33.513", "33.513"), l_o2 = c("0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"), l_hs = c("54.926", "54.926", "54.926", "54.926", "54.926", "54.926", "54.926", "54.926", "54.926", "54.926", "54.926", "54.926", "54.926", "54.926", "54.926"), l_tp = c("340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0" ), l_wdir = c("1.600", "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", "1.600"), l_along = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), l_cross = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), l_mab = c(".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."), l_depth = c(" 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090"), l_temp1 c("11.288", "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", "11.288"), l_temp7 = c("13.675", "13.675", "13.675", "13.675", "13.675", "13.675", "13.675", "13.675", "13.675", "13.675", "13.675", "13.675", "13.675", "13.675", "13.675" ), l_temp14 = c("13.743", "13.743", "13.743", "13.743", "13.743", "13.743", "13.743", "13.743", "13.743", "13.743", "13.743", "13.743", "13.743", "13.743", "13.743")), .Names = c("dive_id", "timestamp", "visability", "r_wvht", "r_dpd", "r_apt", "r_mwd", "r_wtmp", "l_salinity", "l_o2", "l_hs", "l_tp", "l_wdir", "l_along", "l_cross", "l_mab", "l_depth", "l_temp1", "l_temp7", "l_temp14" ), row.names = c(" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", "11", "12", "13", "14", "15"), class = "data.frame") fishTable (first 15 lines) structure(list(name_2009 = c("E. lateralis", "O. pictus", "S. atrovirens", "S. carnatus", "S. caurinus", "S. chrysomelas", "S. miniatus", "S. mystinus", "S. OYT", "S. pinniger", "S. serriceps", "E. jacksoni", "B. frenatus", "H. decagrammus", "S. rastrelliger"), name_2008 = c("ELAT", "OPIC", "SATR", "SCAR", "SCAU", "SCHR", "SMIN", "SMYS", "OYT", "SPIN", "STRE", "EJAC", "BFRE", "HDEC", "SRAS"), description = c("Striped Surfperch,", "Painted Greenling,", "kelp rockfish,", "gopher rockfish,", "copper rockfish,", "black and yellow rockfish,", "vermilion rockfish,", "blue rockfish,", "OYT (Olive/Yellowtail) Rockfish, This species group includes Sebastes serranoides and Sebastes flavidus (Olive/Yellowtail)", "canary rockfish,", "treefish,", "Black Surfperch,", "Kelp Surfperch,", "Kelp Greenling,", "grass rockfish,")), .Names = c("name_2009", "name_2008", "description"), row.names = c(NA, 15L), class = "data.frame") Thank you for your insight and help, Yolande [[alternative HTML version deleted]]
Jean V Adams
2012-Jul-18 17:15 UTC
[R] loop searching the id corresponding to the given index (timestamp)
What is the function index() that you use in this line of code? thisIndex <- as.character(index(diveData_2008[i,])) Is it from some package? Or a function you wrote yourself? I'm trying to run the code you submitted, but I don't have a function called index(). Jean Yolande Tra <yolande.tra@gmail.com> wrote on 07/18/2012 08:47:04 AM:> Hello, > > I have the following loop for two data sets: diveData_2008 and > diveData_2009. It uses two other data: diveCond_all and fishTable. The > problem is at the point to identify the dive_id for the given index(index> is timestamp). It keeps on saying > for the1st loop > Error in fishReport$dive_id[i] <- dive_id : replacement has length zero > for the 2nd loop > Error in fishReport$dive_id[i + j] <- dive_id : > replacement has length zero > > That is where the only problem resides. > Here is the code and below are the first 15 lines of data > > N <- dim(diveData_2009)[1] + dim(diveData_2008)[1] > fishReport <- data.frame(matrix(0, nrow=N, ncol=11)) > names(fishReport) <- c("dive_id", "site", "section", "level","transect",> "depth", "species", "count", "size") > for(i in 1:dim(diveData_2008)[1]){ > # Which dive is this observation from > thisIndex <- as.character(index(diveData_2008[i,])) > dive_id <- > diveCond_all$dive_id[diveCond_all$timestamp==thisIndex] > cat(dive_id, thisIndex, "\n") > # Store the pertinent data in the fish report data.frame > fishReport$dive_id[i] <- dive_id > fishReport$site[i] <- > diveData_2008[i,"site"] > fishReport$level[i] <- > diveData_2008[i,"level"] > fishReport$section[i] <- 0 > fishReport$transect[i] <- > as.numeric(diveData_2008[i,"TRANSECT"]) > fishReport$depth[i] <- > as.numeric(diveData_2008[i,"depth"]) > fishReport$species[i] <- > fishTable$name_2009[fishTable$name_2008==as.character(diveData_2008 > [i,"species"]) > ][1] > fishReport$count[i] <- > as.numeric(diveData_2008[i,"count"]) > fishReport$size[i] <- > as.numeric(diveData_2008[i,"size"]) > fishReport$visibility[i] <- > as.numeric(diveData_2008[i,"VIS_M"]) > fishReport$swell[i] <-as.numeric(diveData_2008[i,"swell_URSKI"]) > } > j<-i > ## The 2009 dives > for(i in i+1:dim(diveData_2009)[1]){ > thisIndex <- as.character(index(diveData_2009[i,])) > dive_id <- > diveCond_all$dive_id[diveCond_all$timestamp==thisIndex] > cat(dive_id, thisIndex, "\n") > fishReport$dive_id[i+j] <- dive_id > fishReport$site[i+j] <-"Hopkins"> fishReport$level[i+j] <- > diveData_2009[i,"level"] > fishReport$transect[i+j] <- > as.numeric(diveData_2009[i,"TRANSECT"]) > fishReport$depth[i+j] <- > as.numeric(diveData_2009[i,"depth"]) > fishReport$species[i+j] <- > diveData_2009[i,"species"] > fishReport$count[i+j] <- > as.numeric(diveData_2008[i,"count"]) > fishReport$size[i+j] <- > as.numeric(diveData_2009[i,"size"]) > fishReport$visibility[i+j] <- > as.numeric(diveData_2008[i,"VIS_M"]) > } > > > diveData_2008 (first 15 lines) > structure(c(" 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", > " 2", " 2", " 2", " 2", " 2", " 2", "8/6/2008", "8/6/2008", "8/6/2008", > "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", > "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", > "8:49", "8:49", "8:49", "8:49", "8:49", "8:49", "8:49", "8:49", > "8:49", "10:03", "10:03", "10:03", "10:03", "10:03", "10:03", > "S. OYT", "S. atrovirens", "S. atrovirens", "S. atrovirens", > "O. pictus", "C. nicholsii", "C. nicholsii", "D. vacca", "No organisms", > "S. OYT", "S. atrovirens", "S. atrovirens", "S. atrovirens", > "S. chrysomelas", "O. pictus", " 15", " 1", " 1", " 1", > " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", > " 1", " 1", " 1", " 6", "23", "27", "29", " 8", " 8", " 9", > "18", NA, " 6", "23", "28", "35", "32", " 8", "Hopkins", "Hopkins", > "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", > "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", > "Hopkins", "15", "15", "15", "15", "15", "15", "15", "15", "15", > "10", "10", "10", "10", "10", "10", "B", "B", "B", "B", "B", > "B", "B", "B", "M", "B", "B", "B", "B", "B", "B", " 1", " 1", > " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 2", " 2", " 2", " 2", > " 2", " 2", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", > " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", > "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", > "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "1.0686708", > "1.0686708", "1.0686708", "1.0686708", "1.0686708", "1.0686708", > "1.0686708", "1.0686708", "1.0686708", "1.1111950", "1.1111950", > "1.1111950", "1.1111950", "1.1111950", "1.1111950"), .indexCLASS > c("POSIXct", > "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", > class = c("xts", > "zoo"), index = structure(c(1218026940, 1218026940, 1218026940, > 1218026940, 1218026940, 1218026940, 1218026940, 1218026940, 1218026940, > 1218031380, 1218031380, 1218031380, 1218031380, 1218031380, 1218031380 > ), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(15L, > 13L), .Dimnames = list(NULL, c("dive_id", "date", "time", "species", > "count", "size", "site", "depth", "level", "TRANSECT", "VIS_M", > "TEMP_C", "swell_URSKI"))) > diveData_2009 (first 15 lines) > structure(c("62", "62", "62", "62", "62", "62", "62", "62", "62", > "62", "62", "62", "62", "62", "62", "10/12/2009", "10/12/2009", > "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", > "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", > "10/12/2009", "10/12/2009", "10/12/2009", "12:44", "12:44", "12:44", > "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", > "12:44", "12:44", "12:44", "12:44", "12:44", "E. lateralis", > "E. lateralis", "E. lateralis", "E. lateralis", "O. pictus", > "S. OYT", "S. OYT", "S. atrovirens", "S. atrovirens", "S. atrovirens", > "S. atrovirens", "S. carnatus", "S. carnatus", "S. carnatus", > "S. carnatus", " 2", " 1", " 1", " 1", " 1", " 3", " 1", " 3", > " 2", " 1", " 1", " 1", " 2", " 1", " 1", "15", "22", "25", "27", > " 4", " 6", " 8", " 5", "20", "23", "25", " 5", "20", "26", "30", > "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", > "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", > "Hopkins", "Hopkins", "Hopkins", "15", "15", "15", "15", "15", > "15", "15", "15", "15", "15", "15", "15", "15", "15", "15", "B", > "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", > "B", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", > "1", "1", "1", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", > "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", > "4.0000", "4.0000", "4.0000"), class = c("xts", "zoo"), .indexCLASS > c("POSIXct", > "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", > index = structure(c(1255365840, > 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, > 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, > 1255365840, 1255365840), tzone = "", tclass = c("POSIXct", "POSIXt" > )), .Dim = c(15L, 11L), .Dimnames = list(NULL, c("dive_id", "date", > "time", "species", "count", "size", "site", "depth", "level", > "TRANSECT", "VIS_M"))) > diveCond_all (first 15 lines) > structure(list(dive_id = structure(1:15, .Label = c(" 1", " 2", > " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", "11", "12", "13", > "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", > "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", > "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", > "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", > "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", > "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", > "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", > "91", "92", "93", "94", "95", "96", "97"), class = "factor"), > timestamp = structure(c(1218026940, 1218031380, 1218032700, > 1218202920, 1218207180, 1218213720, 1218478320, 1218478320, > 1218463800, 1218463800, 1218466860, 1218467760, 1218634260, > 1218638100, 1218639720), class = c("POSIXct", "POSIXt"), tzone =""),> visability = structure(c(5L, 5L, NA, 6L, NA, NA, NA, NA, > NA, NA, 11L, 10L, NA, NA, NA), .Label = c(" 1.5", " 2.0", > " 2.5", " 3.0", " 3.5", " 4.0", " 5.0", " 5.5", " 6.0", " 6.5", > " 7.0", " 7.5", " 8.0", " 9.0", "10.0", "12.0", "13.0", "3.0000", > "4.0000", "5.0000", "5.4864", "6.0000", "6.0960"), class ="factor"),> r_wvht = c(0.91, 1, 1.02, 0.76, 0.78, 0.78, 1.35, 1.35, 1.57, > 1.57, 1.53, 1.53, 1.51, 1.53, 1.69), r_dpd = c(14.29, 4.35, > 4.35, 13.33, 13.33, 8.33, 8.33, 8.33, 8.33, 8.33, 8.33, 8.33, > 9.09, 9.09, 8.33), r_apt = c(4.59, 4.45, 4.26, 5.87, 6.27, > 6.58, 6.7, 6.7, 6.67, 6.67, 6.66, 6.5, 6.99, 6.95, 7.19), > r_mwd = c(229, 291, 297, 226, 228, 260, 287, 287, 291, 291, > 290, 290, 294, 292, 298), r_wtmp = c(12.6, 12.6, 12.5, 12.7, > 12.6, 12.6, 12.8, 12.8, 12.7, 12.7, 12.7, 12.7, 13.4, 13.4, > 13.5), l_salinity = c("33.513", "33.513", "33.513", "33.513", > "33.513", "33.513", "33.513", "33.513", "33.513", "33.513", > "33.513", "33.513", "33.513", "33.513", "33.513"), l_o2 = c("0", > "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", > "0", "0"), l_hs = c("54.926", "54.926", "54.926", "54.926", > "54.926", "54.926", "54.926", "54.926", "54.926", "54.926", > "54.926", "54.926", "54.926", "54.926", "54.926"), l_tp = c("340.0", > "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", > "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0" > ), l_wdir = c("1.600", "1.600", "1.600", "1.600", "1.600", > "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", > "1.600", "1.600", "1.600"), l_along = c(NA_real_, NA_real_, > NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, > NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, > NA_real_), l_cross = c(NA_real_, NA_real_, NA_real_, NA_real_, > NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, > NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), l_mab = c(".", > ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", > ".", "."), l_depth = c(" 9.090", " 9.090", " 9.090", " 9.090", > " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", > " 9.090", " 9.090", " 9.090", " 9.090", " 9.090"), l_temp1 > c("11.288", > "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", > "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", > "11.288", "11.288"), l_temp7 = c("13.675", "13.675", "13.675", > "13.675", "13.675", "13.675", "13.675", "13.675", "13.675", > "13.675", "13.675", "13.675", "13.675", "13.675", "13.675" > ), l_temp14 = c("13.743", "13.743", "13.743", "13.743", "13.743", > "13.743", "13.743", "13.743", "13.743", "13.743", "13.743", > "13.743", "13.743", "13.743", "13.743")), .Names = c("dive_id", > "timestamp", "visability", "r_wvht", "r_dpd", "r_apt", "r_mwd", > "r_wtmp", "l_salinity", "l_o2", "l_hs", "l_tp", "l_wdir", "l_along", > "l_cross", "l_mab", "l_depth", "l_temp1", "l_temp7", "l_temp14" > ), row.names = c(" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", > " 9", "10", "11", "12", "13", "14", "15"), class = "data.frame") > fishTable (first 15 lines) > structure(list(name_2009 = c("E. lateralis", "O. pictus", "S.atrovirens",> "S. carnatus", "S. caurinus", "S. chrysomelas", "S. miniatus", > "S. mystinus", "S. OYT", "S. pinniger", "S. serriceps", "E. jacksoni", > "B. frenatus", "H. decagrammus", "S. rastrelliger"), name_2008 =c("ELAT",> "OPIC", "SATR", "SCAR", "SCAU", "SCHR", "SMIN", "SMYS", "OYT", > "SPIN", "STRE", "EJAC", "BFRE", "HDEC", "SRAS"), description =c("Striped> Surfperch,", > "Painted Greenling,", "kelp rockfish,", "gopher rockfish,", "copper > rockfish,", > "black and yellow rockfish,", "vermilion rockfish,", "blue rockfish,", > "OYT (Olive/Yellowtail) Rockfish, This species group includes Sebastes > serranoides and Sebastes flavidus (Olive/Yellowtail)", > "canary rockfish,", "treefish,", "Black Surfperch,", "Kelp Surfperch,", > "Kelp Greenling,", "grass rockfish,")), .Names = c("name_2009", > "name_2008", "description"), row.names = c(NA, 15L), class ="data.frame")> Thank you for your insight and help, > Yolande[[alternative HTML version deleted]]
Rui Barradas
2012-Jul-18 17:37 UTC
[R] loop searching the id corresponding to the given index (timestamp)
Hello, Try the following. for(i in 1:dim(diveData_2008)[1]){ # Which dive is this observation from thisIndex <- as.character(index(diveData_2008[i,])) thisIndex <- as.POSIXct(thisIndex) dive_id <- diveCond_all$dive_id[diveCond_all$timestamp == thisIndex] # [... etc ...] And the same in the 2009 loop. I think this is it. Good luck, Rui Barradas Em 18-07-2012 14:47, Yolande Tra escreveu:> Hello, > > I have the following loop for two data sets: diveData_2008 and > diveData_2009. It uses two other data: diveCond_all and fishTable. The > problem is at the point to identify the dive_id for the given index (index > is timestamp). It keeps on saying > for the1st loop > Error in fishReport$dive_id[i] <- dive_id : replacement has length zero > for the 2nd loop > Error in fishReport$dive_id[i + j] <- dive_id : > replacement has length zero > > That is where the only problem resides. > Here is the code and below are the first 15 lines of data > > N <- dim(diveData_2009)[1] + dim(diveData_2008)[1] > fishReport <- data.frame(matrix(0, nrow=N, ncol=11)) > names(fishReport) <- c("dive_id", "site", "section", "level", "transect", > "depth", "species", "count", "size") > for(i in 1:dim(diveData_2008)[1]){ > # Which dive is this observation from > thisIndex <- as.character(index(diveData_2008[i,])) > dive_id <- > diveCond_all$dive_id[diveCond_all$timestamp==thisIndex] > cat(dive_id, thisIndex, "\n") > # Store the pertinent data in the fish report data.frame > fishReport$dive_id[i] <- dive_id > fishReport$site[i] <- > diveData_2008[i,"site"] > fishReport$level[i] <- > diveData_2008[i,"level"] > fishReport$section[i] <- 0 > fishReport$transect[i] <- > as.numeric(diveData_2008[i,"TRANSECT"]) > fishReport$depth[i] <- > as.numeric(diveData_2008[i,"depth"]) > fishReport$species[i] <- > fishTable$name_2009[fishTable$name_2008==as.character(diveData_2008[i,"species"]) > ][1] > fishReport$count[i] <- > as.numeric(diveData_2008[i,"count"]) > fishReport$size[i] <- > as.numeric(diveData_2008[i,"size"]) > fishReport$visibility[i] <- > as.numeric(diveData_2008[i,"VIS_M"]) > fishReport$swell[i] <-as.numeric(diveData_2008[i,"swell_URSKI"]) > } > j<-i > ## The 2009 dives > for(i in i+1:dim(diveData_2009)[1]){ > thisIndex <- as.character(index(diveData_2009[i,])) > dive_id <- > diveCond_all$dive_id[diveCond_all$timestamp==thisIndex] > cat(dive_id, thisIndex, "\n") > fishReport$dive_id[i+j] <- dive_id > fishReport$site[i+j] <- "Hopkins" > fishReport$level[i+j] <- > diveData_2009[i,"level"] > fishReport$transect[i+j] <- > as.numeric(diveData_2009[i,"TRANSECT"]) > fishReport$depth[i+j] <- > as.numeric(diveData_2009[i,"depth"]) > fishReport$species[i+j] <- > diveData_2009[i,"species"] > fishReport$count[i+j] <- > as.numeric(diveData_2008[i,"count"]) > fishReport$size[i+j] <- > as.numeric(diveData_2009[i,"size"]) > fishReport$visibility[i+j] <- > as.numeric(diveData_2008[i,"VIS_M"]) > } > > > diveData_2008 (first 15 lines) > structure(c(" 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", > " 2", " 2", " 2", " 2", " 2", " 2", "8/6/2008", "8/6/2008", "8/6/2008", > "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", > "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", "8/6/2008", > "8:49", "8:49", "8:49", "8:49", "8:49", "8:49", "8:49", "8:49", > "8:49", "10:03", "10:03", "10:03", "10:03", "10:03", "10:03", > "S. OYT", "S. atrovirens", "S. atrovirens", "S. atrovirens", > "O. pictus", "C. nicholsii", "C. nicholsii", "D. vacca", "No organisms", > "S. OYT", "S. atrovirens", "S. atrovirens", "S. atrovirens", > "S. chrysomelas", "O. pictus", " 15", " 1", " 1", " 1", > " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 1", > " 1", " 1", " 1", " 6", "23", "27", "29", " 8", " 8", " 9", > "18", NA, " 6", "23", "28", "35", "32", " 8", "Hopkins", "Hopkins", > "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", > "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", > "Hopkins", "15", "15", "15", "15", "15", "15", "15", "15", "15", > "10", "10", "10", "10", "10", "10", "B", "B", "B", "B", "B", > "B", "B", "B", "M", "B", "B", "B", "B", "B", "B", " 1", " 1", > " 1", " 1", " 1", " 1", " 1", " 1", " 1", " 2", " 2", " 2", " 2", > " 2", " 2", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", > " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", " 3.5", > "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", > "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "13.9", "1.0686708", > "1.0686708", "1.0686708", "1.0686708", "1.0686708", "1.0686708", > "1.0686708", "1.0686708", "1.0686708", "1.1111950", "1.1111950", > "1.1111950", "1.1111950", "1.1111950", "1.1111950"), .indexCLASS > c("POSIXct", > "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", > class = c("xts", > "zoo"), index = structure(c(1218026940, 1218026940, 1218026940, > 1218026940, 1218026940, 1218026940, 1218026940, 1218026940, 1218026940, > 1218031380, 1218031380, 1218031380, 1218031380, 1218031380, 1218031380 > ), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(15L, > 13L), .Dimnames = list(NULL, c("dive_id", "date", "time", "species", > "count", "size", "site", "depth", "level", "TRANSECT", "VIS_M", > "TEMP_C", "swell_URSKI"))) > diveData_2009 (first 15 lines) > structure(c("62", "62", "62", "62", "62", "62", "62", "62", "62", > "62", "62", "62", "62", "62", "62", "10/12/2009", "10/12/2009", > "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", > "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", "10/12/2009", > "10/12/2009", "10/12/2009", "10/12/2009", "12:44", "12:44", "12:44", > "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", "12:44", > "12:44", "12:44", "12:44", "12:44", "12:44", "E. lateralis", > "E. lateralis", "E. lateralis", "E. lateralis", "O. pictus", > "S. OYT", "S. OYT", "S. atrovirens", "S. atrovirens", "S. atrovirens", > "S. atrovirens", "S. carnatus", "S. carnatus", "S. carnatus", > "S. carnatus", " 2", " 1", " 1", " 1", " 1", " 3", " 1", " 3", > " 2", " 1", " 1", " 1", " 2", " 1", " 1", "15", "22", "25", "27", > " 4", " 6", " 8", " 5", "20", "23", "25", " 5", "20", "26", "30", > "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", > "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", "Hopkins", > "Hopkins", "Hopkins", "Hopkins", "15", "15", "15", "15", "15", > "15", "15", "15", "15", "15", "15", "15", "15", "15", "15", "B", > "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", > "B", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", > "1", "1", "1", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", > "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", "4.0000", > "4.0000", "4.0000", "4.0000"), class = c("xts", "zoo"), .indexCLASS > c("POSIXct", > "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", > index = structure(c(1255365840, > 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, > 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, 1255365840, > 1255365840, 1255365840), tzone = "", tclass = c("POSIXct", "POSIXt" > )), .Dim = c(15L, 11L), .Dimnames = list(NULL, c("dive_id", "date", > "time", "species", "count", "size", "site", "depth", "level", > "TRANSECT", "VIS_M"))) > diveCond_all (first 15 lines) > structure(list(dive_id = structure(1:15, .Label = c(" 1", " 2", > " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", "11", "12", "13", > "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", > "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", > "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", > "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", > "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", > "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", > "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", > "91", "92", "93", "94", "95", "96", "97"), class = "factor"), > timestamp = structure(c(1218026940, 1218031380, 1218032700, > 1218202920, 1218207180, 1218213720, 1218478320, 1218478320, > 1218463800, 1218463800, 1218466860, 1218467760, 1218634260, > 1218638100, 1218639720), class = c("POSIXct", "POSIXt"), tzone = ""), > visability = structure(c(5L, 5L, NA, 6L, NA, NA, NA, NA, > NA, NA, 11L, 10L, NA, NA, NA), .Label = c(" 1.5", " 2.0", > " 2.5", " 3.0", " 3.5", " 4.0", " 5.0", " 5.5", " 6.0", " 6.5", > " 7.0", " 7.5", " 8.0", " 9.0", "10.0", "12.0", "13.0", "3.0000", > "4.0000", "5.0000", "5.4864", "6.0000", "6.0960"), class = "factor"), > r_wvht = c(0.91, 1, 1.02, 0.76, 0.78, 0.78, 1.35, 1.35, 1.57, > 1.57, 1.53, 1.53, 1.51, 1.53, 1.69), r_dpd = c(14.29, 4.35, > 4.35, 13.33, 13.33, 8.33, 8.33, 8.33, 8.33, 8.33, 8.33, 8.33, > 9.09, 9.09, 8.33), r_apt = c(4.59, 4.45, 4.26, 5.87, 6.27, > 6.58, 6.7, 6.7, 6.67, 6.67, 6.66, 6.5, 6.99, 6.95, 7.19), > r_mwd = c(229, 291, 297, 226, 228, 260, 287, 287, 291, 291, > 290, 290, 294, 292, 298), r_wtmp = c(12.6, 12.6, 12.5, 12.7, > 12.6, 12.6, 12.8, 12.8, 12.7, 12.7, 12.7, 12.7, 13.4, 13.4, > 13.5), l_salinity = c("33.513", "33.513", "33.513", "33.513", > "33.513", "33.513", "33.513", "33.513", "33.513", "33.513", > "33.513", "33.513", "33.513", "33.513", "33.513"), l_o2 = c("0", > "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", > "0", "0"), l_hs = c("54.926", "54.926", "54.926", "54.926", > "54.926", "54.926", "54.926", "54.926", "54.926", "54.926", > "54.926", "54.926", "54.926", "54.926", "54.926"), l_tp = c("340.0", > "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", > "340.0", "340.0", "340.0", "340.0", "340.0", "340.0", "340.0" > ), l_wdir = c("1.600", "1.600", "1.600", "1.600", "1.600", > "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", "1.600", > "1.600", "1.600", "1.600"), l_along = c(NA_real_, NA_real_, > NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, > NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, > NA_real_), l_cross = c(NA_real_, NA_real_, NA_real_, NA_real_, > NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, > NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), l_mab = c(".", > ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", > ".", "."), l_depth = c(" 9.090", " 9.090", " 9.090", " 9.090", > " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", " 9.090", > " 9.090", " 9.090", " 9.090", " 9.090", " 9.090"), l_temp1 > c("11.288", > "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", > "11.288", "11.288", "11.288", "11.288", "11.288", "11.288", > "11.288", "11.288"), l_temp7 = c("13.675", "13.675", "13.675", > "13.675", "13.675", "13.675", "13.675", "13.675", "13.675", > "13.675", "13.675", "13.675", "13.675", "13.675", "13.675" > ), l_temp14 = c("13.743", "13.743", "13.743", "13.743", "13.743", > "13.743", "13.743", "13.743", "13.743", "13.743", "13.743", > "13.743", "13.743", "13.743", "13.743")), .Names = c("dive_id", > "timestamp", "visability", "r_wvht", "r_dpd", "r_apt", "r_mwd", > "r_wtmp", "l_salinity", "l_o2", "l_hs", "l_tp", "l_wdir", "l_along", > "l_cross", "l_mab", "l_depth", "l_temp1", "l_temp7", "l_temp14" > ), row.names = c(" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", > " 9", "10", "11", "12", "13", "14", "15"), class = "data.frame") > fishTable (first 15 lines) > structure(list(name_2009 = c("E. lateralis", "O. pictus", "S. atrovirens", > "S. carnatus", "S. caurinus", "S. chrysomelas", "S. miniatus", > "S. mystinus", "S. OYT", "S. pinniger", "S. serriceps", "E. jacksoni", > "B. frenatus", "H. decagrammus", "S. rastrelliger"), name_2008 = c("ELAT", > "OPIC", "SATR", "SCAR", "SCAU", "SCHR", "SMIN", "SMYS", "OYT", > "SPIN", "STRE", "EJAC", "BFRE", "HDEC", "SRAS"), description = c("Striped > Surfperch,", > "Painted Greenling,", "kelp rockfish,", "gopher rockfish,", "copper > rockfish,", > "black and yellow rockfish,", "vermilion rockfish,", "blue rockfish,", > "OYT (Olive/Yellowtail) Rockfish, This species group includes Sebastes > serranoides and Sebastes flavidus (Olive/Yellowtail)", > "canary rockfish,", "treefish,", "Black Surfperch,", "Kelp Surfperch,", > "Kelp Greenling,", "grass rockfish,")), .Names = c("name_2009", > "name_2008", "description"), row.names = c(NA, 15L), class = "data.frame") > Thank you for your insight and help, > Yolande > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Reasonably Related Threads
- NA instead of time stamp
- readRDS, In as.double.xts(fishReport$count) : NAs introduced by coercion
- Error in as.xts
- replacement has length zero. In addition: Warning message: In max(i) : no non-missing arguments to max; returning -Inf
- help on model selection - step()