-3

How can I write a conditional function that compares entries in rows of a data frame in R. For example if entry in row 1 is greater than entry in row 2 replace row 2 entry with "lower"

row1 row2
667  668    
673  674
665  679 
664  668 

Entries in first column will be higher,lower,lower if the function is applied.

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
Soulkrates
  • 17
  • 1
  • 4

2 Answers2

0

You can use an ifelse:

dat$row2 <- ifelse(dat$row1 < dat$row2, "higher", "lower")
jeremycg
  • 24,657
  • 5
  • 63
  • 74
0

We could do

dat1 <- dat
dat1[] <- c('lower', 'higher')[t(apply(dat, 1, order))]
akrun
  • 874,273
  • 37
  • 540
  • 662