user2631296
user2631296

Reputation: 903

Loop a function over multiple datasets and multiple columns within each dataset

I have a six datasets (set1 to set6), each with columns q1 to q23. I want to print columns 1 and 2 of all of the rows of each dataset if column q has a value less than a threshold value, let's say 0.1.

I am using this statement to print the rows, which works in isolation.

subset(set1, q1 < 0.1, select = (Column1 && Column2))

However, I want to write a nested for loop to loop over sets1-6 and columns 1-23 within each set.

This is the pseudocoded idea that I have, but I recognize that this is not correct R syntax. Help, please?

for (i in 1:6)
{
    for (j in 1:23)
    {
        subset(set[i], q[j] < 0.1, select = (Column1 && Column2))
    }
}

Upvotes: 0

Views: 4112

Answers (1)

Ferdinand.kraft
Ferdinand.kraft

Reputation: 12829

You can use this:

for (i in 1:6)
{
    for (j in 1:23)
    {
        x <- get(paste0("set",i))

        print(x[x[,paste0("q",j)]<0.1, 1:2])
    }
}

Upvotes: 2

Related Questions