我有一个包含很多列的 data.frame,其中一个具有示例区域的代码,另一个具有示例的编号。我想从每个样本区域的倒数第二个样本中对信息进行子集化。我尝试了很多不同的东西......最后这是我最好的猜测......但它仍然无法正常工作。
site <- sample (1:3, 10, replace= T)
d2 <- sample (1:5, 10, replace= T)
d3 <- sample (1:5, 10, replace= T)
samplet <- sample (1:4, 10, replace= T)
mydata <- data.frame (cbind(site, d2, d3, samplet))
penultimate <- matrix(NA,,) # here I dont know how the return will be, as I dont know how the dataframe will change
si <- matrix (NA, , )
pl <- unique (site)
for (i in 1:(length (pl))) {
si <- mydata[which (samplet==pl[i]),] # I tried to create a temporary matrix, so I can calculate each site at a time
penultimate <- si[which (si$samplet!=(max(si$samplet[si$samplet!=max(si$samplet)]))),]
}
干杯!