Henry
Henry

Reputation: 1686

Maximum value row-by-row R data frame

I have a data frame in R, which looks something like:

df<-data.frame(c(1:10))
colnames(df)<-'Val'
df['Max']<-max(df$Val,5)

Desired output would be:

row, Val, Max
1  ,   1,  5
2  ,   2,  5
3  ,   3,  5
4  ,   4,  5
.....
6   ,   6, 6

etc etc

This is currently just giving me 10 in each row of Max. Help appreciated, thanks

Upvotes: 2

Views: 884

Answers (1)

akrun
akrun

Reputation: 887118

We can use pmax

df$Max <- pmax(df[,1], 5)
df
#   Val Max
#1    1   5
#2    2   5
#3    3   5
#4    4   5
#5    5   5
#6    6   6
#7    7   7
#8    8   8
#9    9   9
#10  10  10

Upvotes: 4

Related Questions