jeudi 16 août 2018

Why is my conditional function not working for my data table?

My overall goal is this:

  • iterate through each row in one data table
  • find the rows in the second data table that have matching values in certain columns from row in first data table
  • for each row in second data table, compare value from one column to value from corresponding column in first, if they do match, assign value of 0, if not assign value of 1 to new column
  • for each row in first data table, sum up values created and divide by number of matching rows in second data table. Assign this value to new column in first data frame.

The structure of my merged data table (df3) is shown below.

lapply(df3, class)

$`A E I`
'character'
$`U3 I`
'character'
$`H D`
'Date'
$`E I`
'character'
$E_Z
'character'
$S_L
'numeric'

nrow(df3)
3054059

My problem is that my conditional statement is assigning false to the conditional statement even if the values do match.

 setDT(dfLS)
  setDT(dfS2)    
  setkey(dfS, E_I, H_D, A_E_I)
  setkey(dfLS2, "E I","H D","A E I")
  df3<-dfLS2[dfLS]
  df3<-df3[, S_L := ifelse("E_Z" == "U3 I", 0, 1), by=.I]
  dfLSLoc <- as.data.frame(df3[, .(sum(S_L, na.rm = TRUE)/.N),by = c("E I","H D","A E I")])

It seems I've tried every possible way to make it match but for some reason it returns false no matter what?

Aucun commentaire:

Enregistrer un commentaire