R 函数中的向量长度误差

R 函数中的向量长度误差,r,R,我正在努力调试这个函数,非常感谢您的帮助。问题出在这一行: thres <- resultsHt$iithreshold 这就是功能: 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

我正在努力调试这个函数,非常感谢您的帮助。问题出在这一行:

thres <- resultsHt$iithreshold
这就是功能:

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
库(data.table)

dataDT
x的长度是多少警告意味着
x
thres
的长度不同,因此R的向量运算循环规则可能没有达到您预期的效果。谢谢。我不知道如何计算x的长度。不幸的是,我得到了创建函数的帮助。麻烦你帮我查一下好吗?我知道thres的长度是40,这是正确的。如果有帮助的话,长度(dataDT)是41。dataDT的第一列是“CAG”,使用
debug(fun\u name)
访问函数,在它执行带故障的行之前,您可以验证它们的长度。谢谢。你要的电话线是1078。浏览[2]>length(dataDT[,colname,with=F][[1]])[1]1078
x的长度是多少警告意味着
x
thres
的长度不同,因此R的向量操作循环规则可能没有达到您预期的效果。谢谢。我不知道如何计算x的长度。不幸的是,我得到了创建函数的帮助。麻烦你帮我查一下好吗?我知道thres的长度是40,这是正确的。如果有帮助的话,长度(dataDT)是41。dataDT的第一列是“CAG”,使用
debug(fun\u name)
访问函数,在它执行带故障的行之前,您可以验证它们的长度。谢谢。你要的电话线是1078。浏览[2]>length(dataDT[,colname,with=F][[1]])[1]1078
> 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