如果你要考慮使用的數據幀的列表,這樣的事情可能實現你以後
Depth <- c(40,60,70,80,85,90)
D2H <- c(-60,-65,-63,-67,-58,-66)
A1 <- data.frame(Depth, D2H)
set.seed(123) ## these would be your other 7 data frames in your case
A2 <- A1[sample(nrow(A1), nrow(A1), replace = TRUE),]
A3 <- A1[sample(nrow(A1), nrow(A1), replace = TRUE),]
A4 <- A1[sample(nrow(A1), nrow(A1), replace = TRUE),]
A5 <- A1[sample(nrow(A1), nrow(A1), replace = TRUE),]
A6 <- A1[sample(nrow(A1), nrow(A1), replace = TRUE),]
A7 <- A1[sample(nrow(A1), nrow(A1), replace = TRUE),]
A8 <- A1[sample(nrow(A1), nrow(A1), replace = TRUE),]
mydflist <- list(A1, A2, A3, A4, A5, A6, A7, A8)
newlist40 <- lapply(mydflist, function(x) subset(x$D2H, x$Depth == 40))
newlist60 <- lapply(mydflist, function(x) subset(x$D2H, x$Depth == 60))
編輯
newlist40.b <- lapply(newlist40, function(x) if(length(x)==0) x <- NA else x)
newlist40.final <- unlist(lapply(newlist40.b, unique)) ## if you want unique values from each array in list
# newlist40.final <- unlist(newlist40.b) # if you don't
newlist60.b <- lapply(newlist60, function(x) if(length(x)==0) x <- NA else x)
newlist60.final <- unlist(lapply(newlist60.b, unique)) ## if you want unique values from each array in list
# newlist60.final <- unlist(newlist60.b) # if you don't
那麼你會得到這種方法的錯誤?您可以嘗試使用'which()'來查找滿足條件的行,然後使用'length()'檢查令人滿意的行數。只有當長度大於'零'時,纔可以嘗試'subset()'。 – TUSHAr