功能中的矢量长度错误

时间:2017-07-06 19:26:08

标签: r

我真的很难调试这个功能,非常感谢你的帮助。问题出在这一行:

thres <- resultsHt$iithreshold

错误是:

There were 40 warnings (use warnings() to see them)
1: In x < (thres * max(x)) :
 longer object length is not a multiple of shorter object length
etc...

然而,我的矢量是正确的长度:

> length(resultsHt$iithreshold)
[1] 40

这是功能:

library(data.table)
dataDT <- height[,13:ncol(height)] #Create a data frame containing CAG and height columns
dataDT <- setDT(dataDT) #Convert to a data table

colsToBeUsed<-names(dataDT[,!'CAG']) #Assigns the columns to be analysed

myFun <- function(x, mod, cag, thres) { #Function that takes vectors as arguments.
  x[x < (thres * max(x))] <- 0 #First sets all heights < 0.2*threshold to 0.
  norm_x <- x / sum(x) #Then normalises heights by dividing by the sum of the heights. 
  sum_x <- norm_x * (cag - mod) #Then multiplies by the change in CAG from mode
  sum(sum_x) #Then sums the results
} 

transision_matrix <- merge(propsettings, modeHt, by.x = "control", by.y = "sample") #Transition matrix that determines control modes for each sample
transision_matrix$mode <- resultsHt$mode #Adapting function for control modes to use sample modes (opposite to the way I normally develop the code). This allows 'mod' to be a vector of sample modes rather than control modes
setDT(transision_matrix)

mf2 <- function(colname, dataDT, transision_matrix){ #Function that takes column name and data table as arguments.
  x <- dataDT[, colname, with = F][[1]]
  mod <- transision_matrix[sample == colname, mode] #Vector of CONTROL modes to use
  cag <- dataDT[, "CAG"][[1]]
  thres <- resultsHt$iithreshold
  myFun(x, mod, cag, thres)
}

iiHt <- sapply(colsToBeUsed, function(x) mf2(x, dataDT, transision_matrix)) #Apply the mf2 function over the column name vector

resultsHt$iiHt <- iiHt

我没有包含所有表格和变量,但如果有用,请告诉我

0 个答案:

没有答案