user2065472
user2065472

Reputation: 3

Apply function across mulitple columns in R

I'm trying to write a streamlined function in R to compare multiple columns in a matrix. What is the optimal way to do this in R? Most likely using apply.

I have seen this question crop up a number of times but with some conflicting views on the optimal way to write this.

for ( j in 2:ncol(net) )
{
    for ( i in 1:nrow(net) )
    {
            net[i,j] <- min(net[i,j],net[i,1])
    }
}

The end output of a matrix with the following

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    2    3
[3,]    3    2    3

would be

     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    2    3

Upvotes: 0

Views: 145

Answers (3)

alistaire
alistaire

Reputation: 43354

Here's a version with sapply and ifelse (which is vectorised, woo), which is likely faster, and deals with NA values in a predictable way:

sapply(X = seq(to = ncol(x = net)), FUN = function(j){
  net[,j] <- ifelse(test = net[,1] < net[,j], yes = net[,1], no = net[,j])
})

Some sample data

net <- head(airquality)
net
  Ozone Solar.R Wind Temp Month Day
1    41     190  7.4   67     5   1
2    36     118  8.0   72     5   2
3    12     149 12.6   74     5   3
4    18     313 11.5   62     5   4
5    NA      NA 14.3   56     5   5
6    28      NA 14.9   66     5   6

results in:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]   41   41  7.4   41    5    1
[2,]   36   36  8.0   36    5    2
[3,]   12   12 12.0   12    5    3
[4,]   18   18 11.5   18    5    4
[5,]   NA   NA   NA   NA   NA   NA
[6,]   28   NA 14.9   28    5    6

Note: I specified pretty much all argument names, as I've found this makes most code faster. If you don't care about time, a simpler [possibly more readable] version:

sapply(seq(ncol(net)), function(j){
    net[,j] <- ifelse(net[,1] < net[,j], net[,1], net[,j])
})

Upvotes: 0

akrun
akrun

Reputation: 887901

We can unlist the columns the "net" except the first column (net[-1]), replicate the first column as the same length as the unlisted columns, and use pmin to get the minimum value of corresponding elements of the vectors.

pmin(unlist(net[-1], use.names=FALSE), net[,1][row(net[-1])])
#[1] 2 2 7 5 2 2 2 6 5 3 2 1 0 5 1

If we need a lapply solution,

unlist(lapply(net[-1], function(x) pmin(x, net[,1])), use.names=FALSE)

Using the OP's for loop

for ( i in 2:ncol(net) ){
   for ( j in 1:nrow(net) ){
     print(min(net[j,i],net[j,1]))
   }
 }
#[1] 2
#[1] 2
#[1] 7
#[1] 5
#[1] 2
#[1] 2
#[1] 2
#[1] 6
#[1] 5
#[1] 3
#[1] 2
#[1] 1
#[1] 0
#[1] 5
#[1] 1

Update

As the OP mentioned that this is not giving the expected output, trying with new data showed in the OP's post

net <- cbind(1:3, 2, 3)

cbind(net[,1],pmin(unlist(net[,-1], use.names=FALSE), 
           net[,1][row(net[,-1])]))
#      [,1] [,2] [,3]
#[1,]    1    1    1
#[2,]    2    2    2
#[3,]    3    2    3

data

set.seed(24)
net <- as.data.frame(matrix(sample(0:9, 4*5, replace=TRUE), ncol=4))

Upvotes: 1

jogo
jogo

Reputation: 12569

If there are no NAs you can do

net <- head(airquality, 4) # example data
for (j in 1:nrow(net)) net[j, net[j,]>net[j,1]] <- net[j,1]
net

Upvotes: 0

Related Questions