user2543622
user2543622

Reputation: 6776

rowwise maximum for R

I have a dataframe as below. I want to get a column of maximums for each row. But that column should ignore value 9 if it is present in that row. How can i achive that efficiently?

df <- data.frame(age=c(5,6,9), marks=c(1,2,7), story=c(2,9,1))
df$max <- apply(df, 1, max)    
df

Upvotes: 34

Views: 67804

Answers (4)

MrFlick
MrFlick

Reputation: 206401

The pmax function would be useful here. The only catch is that it takes a bunch of vectors as parameters. You can convert a data.frame to parameters with do.call. I also set the 9 values to NA as suggested by other but do so using the somewhat unconventional is.na<- command.

do.call(pmax, c(`is.na<-`(df, df==9), na.rm=T))
# [1] 5 6 7

Upvotes: 23

talat
talat

Reputation: 70296

Here's one possibility:

df$colMax <- apply(df, 1, function(x) max(x[x != 9]))

Upvotes: 27

Roland
Roland

Reputation: 132874

Substitute 9 with NA and then use pmax as suggested by @MrFlick in his deleted answer:

df2 <- df #copy df because we are going to change it
df2[df2==9] <- NA
do.call(function(...) pmax(..., na.rm=TRUE), df2)
#[1] 5 6 7

Upvotes: 4

russellpierce
russellpierce

Reputation: 4711

#make a copy of your data.frame
tmp.df <- df
#replace the 9s with NA
tmp.df[tmp.df==9] <- NA
#Use apply to process the data one row at a time through the max function, removing NA values first
apply(tmp.df,1,max,na.rm=TRUE)

Upvotes: 2

Related Questions