-1

I'm trying to run this script:

input <- read.csv ("trio.csv")

for (i in 1:nrow(input)) {
  if (input[i, 6] == input[i, 2]) {
    if (input[i, 7] == input[i, 4] |
        input[i, 7] == input[i, 7]) {
      print ("h1")
    } else {
      print ("not h1")
    }
  } else if (input[i, 6] == input[i, 3]) {
    if (input[i, 7] == input[i, 4] | input[i, 7] == input[i, 7]) {
      print ("h1")
    } else {
      print ("not h1")
    }
  } else {
    print("not h1")
  }
}

for (j in 1:nrow(input)) {
  if (input[j, 7] == input[j, 2]) {
    if (input[j, 6] == input[j, 4] | input[j, 6] == input[j, 7]) {
      print ("h2")
    } else {
      print ("not h2")
    }
  } else if (input[j, 7] == input[j, 3]) {
    if (input[j, 6] == input[j, 4] |input[j, 6] == input[j, 7]) {
      print ("h2")
    } else {
      print ("not h2")
    }
  } else {
    print("not h2")
  }
}

The trio.csv file is composed of 7 columns and 100 rows, here is an example of what trio.csv looks like:

dput(head(input))

structure(list(familyID = c(1001L, 1004L), Pat1 = structure(2:1, .Label = 
c("REF", "X1"), class = "factor"), Pat2 = structure(1:2, .Label = c("C2", 
"REF"), class = "factor"), Mat1 = structure(1:2, .Label = c("C2", 
"REF"), class = "factor"), Mat2 = structure(c(1L, 1L), .Label = "C2", 
class  = "factor"), 
Ch1 = structure(2:1, .Label = c("REF", "X1"), class = "factor"), 
Ch2 = structure(1:2, .Label = c("C2", "REF"), class = "factor")), 
row.names = 1:2, class = "data.frame") 

How can I save the results from the first and the second loops in two different columns in a new file ?

Nzar
  • 1
  • 2
  • Please fix your formatting. – Tim Biegeleisen Oct 17 '18 at 16:00
  • 1
    Ugh, `print` is not saving into a new column; `print` should very rarely be used in a function (imho) except as a method for putting something on the console outside of the data. Since we don't know what `trio.csv` looks like, I suggest you rephrase the question to include (a) a *sample* of the data, perhaps `dput(head(input))`; (b) format your code so that it doesn't extend 100+ characters wide (readability); and (c) ensure that your expected output is sufficiently covered by your sample data. For reproducibility examples, see (among others): https://stackoverflow.com/questions/5963269 – r2evans Oct 17 '18 at 16:31
  • Thank you guys for the editing you made, this is my first ever written script in r. I have added a sample data of trio.csv to the question. – Nzar Oct 18 '18 at 10:42

1 Answers1

0

This is how I managed to save results from both loops into two new csv files, and it works for me. All suggestions to make more efficient are welcome. Thank you

input <- read.csv ("trio.csv")

b <- matrix( , 0,ncol=2, )
d <- matrix( , 0,ncol=2, )

for (i in 1:nrow(input)) {
  x= if (input[i, 6] == input[i, 2]) {
    if (input[i, 7] == input[i, 4] |
        input[i, 7] == input[i, 5]) {
      print ("h1")
    } else {
      print ("not h1")
    }
  } else if (input[i, 6] == input[i, 3]) {
    if (input[i, 7] == input[i, 4] | input[i, 7] == input[i, 5]) {
      print ("h1")
    } else {
      print ("not h1")
    }
  } else {
    print("not h1")
  } 
    b <- rbind(b,c(i, x))
}

for (j in 1:nrow(input)) {
  y= if (input[j, 7] == input[j, 2]) {
    if (input[j, 6] == input[j, 4] | input[j, 6] == input[j, 5]) {
      print ("h2")
    } else {
      print ("not h2")
    }
  } else if (input[j, 7] == input[j, 3]) {
    if (input[j, 6] == input[j, 4] |input[j, 6] == input[j, 5]) {
      print ("h2")
    } else {
      print ("not h2")
    }
  } else {
    print("not h2")
  } 
    d<- rbind(d, c(j, y))
}


write.table(b,"hyp1.csv",sep=",",row.names = FALSE,col.names = FALSE)
write.table(d,"hyp2.csv",sep=",",row.names = FALSE,col.names = FALSE)
Nzar
  • 1
  • 2