darkage
darkage

Reputation: 857

using lapply in R

I have a data frame with 10 variables, which obviously has some data issues. E.g, say column1 should not be greater than column2, but in the data we have some entries which violate this. So, we assume that if column1>column2, replace the value in column1 with the corresponding value in column2. Now I want to compare say col1,col3,col4 with col2 and apply the same logic as above to all the columns. I have used the ifelse function in R, like

data$col1 <- ifelse (data$col1>data$col2,data$col2,data$col1)

this works fine.

But is there a way by which I can achieve the same for all the cols (i.e., col1,col3and col4) at once? I think it can be done using lapply, but not quite sure how.

Upvotes: 1

Views: 239

Answers (2)

BrodieG
BrodieG

Reputation: 52687

This version sets all values in any column other than 2 to be less than or equal to values in column 2. First, make toy data:

df <- as.data.frame(replicate(10, sample(1:10), simplify=F))
names(df) <- paste0("col", 1:10)

Now, use lapply to achieve your objective, replacing all columns other than 2 by the capped value:

df[-2] <- lapply(df[-2], function(x) ifelse(x > df[[2]], df[[2]], x))
df

It's not clear exactly what you're trying to do, but if you can clarify I can provide a more targeted answer. Either way hopefully this gives you some ideas.

Upvotes: 1

Rcoster
Rcoster

Reputation: 3210

You can do with a for:

set.seed(31415)
(data <- data.frame(matrix(rnorm(100), ncol=10)))

for (i in c(1, 3, 4, 7)) { # Let's check columns 1, 3, 4 and 7
data[, i] <- ifelse(data[, i] > data[, 2], data[, 2], data[, i])
}
data

Upvotes: 0

Related Questions