有人告诉我,在R中使用for循环是个坏主意,最好使用sapply。prob <- (days - i + 1)/days * prob # Formula for no coincidence}1 - sapply(1:length(m), function(x) prod(m[1:x]))
但是,如何使用公式,而不
我试图用sapply和function(x,y)来循环我的矩阵。test<-sapply(colnames(my_mat), function(x, y) ifelse (x==y,-1,cor(test_mat[,x],test_mat[,y], use="p")) )
基本上,它计算每个列单元格之间的相关性,但当它们都是相同的对象时,我希望它返回-1而不是1。我在说Error in x == y : 'y' is missing时遇到了一个错误,但是如果我
但是,我对python中的sapply等效项并不是很熟悉。这部分代码结合了for循环和sapply。length(num)) for ( i in 1:length(num)) { data[,i] <- sapply} error_1 <- "X"
} 此代码块中未定义的变量num只是df$train_nbr的唯一值的列表 我想我