0

I am currently trying to read out data from multiple data frames (A1-A8), which all have the same structure, with a for loop. Within the for loop, I would like to extract data meeting a certain condition (in this case, a specific depth). I tried this with subset and if-else. The problem I am facing is, that in case this condition is not met, the for loop is interrupted and the following steps are not carried out. Here's the example:

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)
}

So if for example data frame A3 doesn't contain Depth = 40, the for loop is interrupted. How can I overcome this problem? Wrong approach?

I hope I described the problem well enough, if not, let me know and I will extend the description. I am highly thankful for any recommendations.

1
  • 1
    so are you getting an error with this approach? You can try using the which() to find the rows which satisfy the conditions and then check the number of satisfactory rows using length(). Only if the length is greater than 'zero', try the subset(). Commented Jul 25, 2017 at 9:49

2 Answers 2

1

If you want to consider using a list of data frames, something like this might achieve what you're after

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))

EDIT

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
Sign up to request clarification or add additional context in comments.

5 Comments

Great! Thanks! The only issue is that I get a list instead of a vector as a result. Is there a way to get a vector directly?
you want a vector with unique values whatever the data.frame they're coming from? what should the vector look like?
basically a vector with the 8 values I was reading out of the dataframes, best with NA if there was none.
Yes, very nice! Thanks alot! Great help!
consider accepting the question if that solves your problem
0

I think simone's way is the best answer, but if you are determined to use a for loop, the fix here has to do with just writing an if condition.

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 }
}

1 Comment

Yes, this also works great. I´ve been trying to use the if function, but couldn´t figure out how. This looks much better! Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.