2

I have a data frame and I want to replace variables with some codes if they are more than some values else with some values

jc<-for (i in 1:nrow(j1)) {
   if (j1[i,2]<=150){
     j1[i,2]=1
  }else if (j1[i,2]<=250){
    j1[i,2]= 2
   }
   }

the dput output I am giving here dput(j1[1:5])

structure(list(OUTPUT_NAME = c("nonsaturation_fba268_2ch_0_out.wav", 
"nonsaturation_fba268_2ch_32_out.wav", "substreaminfo_fba268_2ch_96_out.wav", 
"substreaminfo_fba268_2ch_201_out.wav", "substreaminfo_fba268_2ch_93_out.wav"
), PEAK_MIPS = c(82.47, 82.5, 82.63, 82.73, 82.73), X1 = c(0, 
0, 0, 0, 0), X2 = c(0, 0, 0, 0, 0), X3 = c(1, 1, 1, 1, 1), X4 = c(31, 
31, 31, 31, 31), X5 = c(0, 0, 0, 0, 0), X6 = c(0, 0, 0, 0, 0), 
    X7 = c(1, 1, 1, 1, 1), X8 = c(1, 1, 13, 1, 1), X9 = c(1, 
    1, 13, 1, 1), X10 = c(0, 0, 0, 0, 0), X11 = c(0, 0, 0, 0, 
    0), X12 = c(0, 0, 0, 0, 0), X13 = c(0, 0, 0, 0, 0), X14 = c(9, 
    9, 9, 9, 9), X15 = c(7, 7, 7, 7, 7), X16 = c(4, 4, 6, 6, 
    6), X17 = c(0, 0, 0, 0, 0), X18 = c(16, 16, 8, 8, 8), X19 = c(0, 
    0, 0, 0, 0), X20 = c(1, 1, 1, 1, 1), X21 = c(14, 14, 12, 
    12, 12), X22 = c(1, 1, 0, 0, 0), X23 = c(0, 0, 0, 0, 0), 
    X24 = c(14, 14, 12, 12, 12), X25 = c(0, 0, 0, 0, 0), X26 = c(937, 
    937, 937, 937, 937), X27 = c(1, 1, 1, 1, 1), X28 = c(6000, 
    6000, 6000, 6000, 6000), X29 = c(1, 1, 2, 2, 2), X30 = c(0, 
    0, 0, 0, 0), X31 = c(20, 20, 40, 24, 24), X32 = c(0, 0, 0, 
    0, 0), X33 = c(0, 0, 0, 0, 0), X34 = c(0, 0, 0, 0, 0), X35 = c(0, 
    0, 0, 0, 0), X36 = c(35, 35, 35, 35, 35), X37 = c(35, 35, 
    35, 35, 35), X38 = c(35, 35, 35, 35, 35), X39 = c(31, 31, 
    31, 31, 31), X40 = c(31, 31, 31, 31, 31), X41 = c(31, 31, 
    31, 31, 31), X42 = c(0, 0, 0, 0, 0), X43 = c(0, 0, 0, 0, 
    0), X44 = c(0, 0, 0, 0, 0), X45 = c(28, 28, 31, 31, 31), 
    X46 = c(35, 35, 35, 35, 35), X47 = c(16, 16, 16, 16, 16), 
    X48 = c(1, 1, 1, 1, 1), X49 = c(0, 0, 0, 0, 0), X50 = c(1, 
    1, 0, 0, 0), X51 = c(0, 0, 0, 0, 0), X52 = c(0, 0, 0, 0, 
    0), X53 = c(1, 1, 1, 1, 1), X54 = c(12778, 12778, 12778, 
    12778, 12778), X55 = c(1, 1, 1, 1, 1), X56 = c(0, 0, 0, 0, 
    0), X57 = c(1, 1, 1, 1, 1), X58 = c(0, 0, 1, 1, 1), X59 = c(32, 
    32, 40, 40, 40), X60 = c(0, 0, 0, 0, 0), X61 = c(0, 0, 0, 
    0, 0), X62 = c(0, 0, 0, 0, 0), X63 = c(1, 1, 0, 2, 2), X64 = c(6, 
    6, 8, 5, 5), X65 = c(0, 3, 0, 3, 0), X66 = c(40, 40, 40, 
    40, 40), X67 = c(0, 0, 0, 0, 0)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

I want a condition-based replacement of the second-column values

P Initiate
  • 79
  • 5
  • 1
    Do you need `j$PEAK_MIPS <- ifelse(j$PEAK_MIPS <= 150, 1, 2)` ? – Ronak Shah Dec 13 '19 at 05:55
  • I need two more conditions in the same statement like if it is <=150 print 1 , if >150 and <=300, print 2 else print 3. can you help me in that – P Initiate Dec 13 '19 at 06:20
  • 1
    Look at `?cut`, https://stackoverflow.com/questions/13559076/convert-continuous-numeric-values-to-discrete-categories-defined-by-intervals – Ronak Shah Dec 13 '19 at 06:23
  • 1
    Otherwise simply use 'ifelse', rather than if and else. It is much easier. – Neeraj Dec 13 '19 at 06:31

1 Answers1

0

We can use findInterval

findInterval(j1$PEAK_MIPS, c(150, 300) + 1)
akrun
  • 874,273
  • 37
  • 540
  • 662