我目前正在尝试从多个数据帧(A1-A8
)读取数据,这些数据帧都具有相同的结构,并带有for循环。在for循环中,我想提取满足特定条件的数据(在这种情况下,特定深度)。我用子集和if-else尝试了这个。我面临的问题是,如果不满足此条件,for循环将被中断,并且不执行以下步骤。这是一个例子:
Depth <- c(40,60,70,80,85,90)
D2H <- c(-60,-65,-63,-67,-58,-66)
A1 <- data.frame(Depth, D2H)
for (i in 1:8) {
Ax <- get((paste("A",i,sep="")))# reads in the dataframe
A_40[i] <- subset(Ax$D2H, Ax$Depth == 40) #writes the value of D2H at depth 40 into the new vector
A_60[i] <- subset(Ax$D2H, Ax$Depth == 60)
}
因此,例如,如果数据框A3
不包含Depth = 40
,则for循环被中断。我怎样才能克服这个问题?错误的做法?
我希望我能够很好地描述这个问题,如果没有,请告诉我,我会扩展说明。我非常感谢任何建议。
答案 0 :(得分:1)
如果您想考虑使用数据框列表,这样的事情可能会实现您的目标
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
答案 1 :(得分:0)
我认为simone的方式是最好的答案,但如果您决定使用for循环,那么此处的修复只需编写if条件。
for (i in 1:8) {
Ax <- get((paste("A",i,sep="")))# reads in the dataframe
if (length(subset(Ax$D2H, Ax$Depth == 40) > 0) {A_40[i] <- subset(Ax$D2H, Ax$Depth == 40)
} else { A_40[i] = NA }
if (length(subset(Ax$D2H, Ax$Depth == 60) > 0) {A_60[i] <- subset(Ax$D2H, Ax$Depth == 60)
} else { A_60[i] = NA }
}