lundi 9 novembre 2015

using crossprod under specific conditions

I am trying to organise a dataset in a very specific way for my research, however I am new to R and I am really struggling, any assistance would be greatly appreciated.

I am attempting to take the value of the cell at every third column (starting from the first one) and multiply it by the column beside it, but only if there is a negative value in said cell. Following this, I would like to sum the results together and store it in a new column in an external spreadsheet.

so far the code I have written is as follows:

 NegTotal = NULL 
    p = NULL
    for (i in 1:nrow(Datafile)) 
      {for (j in 1:ncol(Datafile)) 
           {if ((j %% 3 == 0) && (Datafile [i,j] < 0)) {
               p <- (datafile[i,j] * datafile[i,j+1])
               NegTotal <- sum(p) }
           else { }
          }
       } 
   for (l in seq(along = NegTotal)) {
      dim(newColumn)
      AsNewData.DataColumn("datafile", GetType(System.String))
      NewColumn.DefaultValue = "NegTotal"
      table.Columns.Add(newColumn)
    }

I am aware that this code is probably completely wrong, this is the first time I've used R and I am not very proficient at computer programming in general.

Thanks!

Aucun commentaire:

Enregistrer un commentaire