-1

i need some help with my if else function. I want R to print for every raw in my column (DipE) the corresponding value defined in my if else function.

This is my column:

TierLID DipE
MLS289 -4.0020
MLS440 -3.9680
MLS382 -3.9280
MLS414 -3.9280
MLS290 -2.8480
MLS175 -2.5210
MLS232 -2.5020
MLS241 -2.5020
MLS189 -2.4040
MLS563 -2.4040
MLS209 -2.4000
MLS277 -0.7396
MLS514 -0.5619
MLS539 -0.4518
MLS540 -0.4518
MLS200 -0.3709
MLS340 -0.2090

This is my code:

if(Sheeps_with_Haplotyp10$DipE >-4 & <-3.5){
  print("1")
  }else if (Sheeps_with_Haplotyp10$DipE >-3.5 & <-3.0){
  print("3")
  }else if (Sheeps_with_Haplotyp10$DipE >-3.0 & <-2.5){
  print("0")
  }else if (Sheeps_with_Haplotyp10$DipE >-2.5 & <-2.0){
  print("4")
  }else if (Sheeps_with_Haplotyp10$DipE >-2.0 & <-1.5){
  print("3")
  }else if (Sheeps_with_Haplotyp10$DipE >-1.5 & <-1.0){
  print("0")
  }else if (Sheeps_with_Haplotyp10$DipE >-1.0 & <-0.5){
  print("2")
  }else if (Sheeps_with_Haplotyp10$DipE >-0.5 & <-0.0){
  print("4")
  }

all it prints is just 4

Domi
  • 15
  • 2
  • Try `Sheeps_with_Haplotyp10$DipE >-3.5 & Sheeps_with_Haplotyp10$DipE <-3.0` – user2974951 Mar 04 '21 at 08:46
  • 1
    Does this answer your question? [How does cut with breaks work in R](https://stackoverflow.com/questions/39123458/how-does-cut-with-breaks-work-in-r) – fabla Mar 04 '21 at 08:51
  • 1
    Note: `<-` is the assignment operator. – Roland Mar 04 '21 at 08:51
  • 1
    If you are using `if`/`else` you'll need for loop to go over each row. You can use `ifelse`/`cut`/`findInterval` as vectorized options. See these examples https://stackoverflow.com/questions/4126326/how-to-quickly-form-groups-quartiles-deciles-etc-by-ordering-columns-in-a/4126475#4126475 and https://stackoverflow.com/questions/12979456/categorize-numeric-variable-into-group-bins-breaks/12979557#12979557 – Ronak Shah Mar 04 '21 at 09:42

1 Answers1

0

You can't do "Sheeps_with_Haplotyp10$DipE >-1.5 & < -1.0" and expect the > and < to work twice on the thing of interest, you have to do Sheeps_with_Haplotyp10$DipEp[i] >-1.5 & Sheeps_with_Haplotyp10$DipEp[i] < -1.0. In real life, people would understand what you mean, but not the computer. Fortunately, we don't even need to have two checks here because of the cascading if else statements.

text="TierLID   DipE
MLS289  -4.0020
MLS440  -3.9680
MLS382  -3.9280
MLS414  -3.9280
MLS290  -2.8480
MLS175  -2.5210
MLS232  -2.5020
MLS241  -2.5020
MLS189  -2.4040
MLS563  -2.4040
MLS209  -2.4000
MLS277  -0.7396
MLS514  -0.5619
MLS539  -0.4518
MLS540  -0.4518
MLS200  -0.3709
MLS340  -0.2090"
Sheeps_with_Haplotyp10=read.table(text=text, header=TRUE, stringsAsFactors = FALSE)

for (i in 1:nrow(Sheeps_with_Haplotyp10)) {
  if(Sheeps_with_Haplotyp10$DipE[i]< -3.5){
    print("1")
  }else if (Sheeps_with_Haplotyp10$DipE[i] < -3.0){
    print("3")
  }else if (Sheeps_with_Haplotyp10$DipE[i] < -2.5){
    print("0")
  }else if (Sheeps_with_Haplotyp10$DipE[i] < -2.0){
    print("4")
  }else if (Sheeps_with_Haplotyp10$DipE[i] < -1.5){
    print("3")
  }else if (Sheeps_with_Haplotyp10$DipE[i] < -1.0){
    print("0")
  }else if (Sheeps_with_Haplotyp10$DipE[i] < -0.5){
    print("2")
  }else {
    print("4")
  }
}

[1] "1"
[1] "1"
[1] "1"
[1] "1"
[1] "0"
[1] "0"
[1] "0"
[1] "0"
[1] "4"
[1] "4"
[1] "4"
[1] "2"
[1] "2"
[1] "4"
[1] "4"
[1] "4"
[1] "4"
Vons
  • 3,277
  • 2
  • 16
  • 19