Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 样本/物种转换代码帮助_R_Optimization_Matrix_Transformation - Fatal编程技术网

R 样本/物种转换代码帮助

R 样本/物种转换代码帮助,r,optimization,matrix,transformation,R,Optimization,Matrix,Transformation,我试图简化R中的一项任务。我有一个这样的社区矩阵: row.name species1 species2 species3 species4 .... species50 sample 1 1 6 156 4 1 sample 2 0 20 34 5 1 sample 3 3 7 23 0

我试图简化R中的一项任务。我有一个这样的社区矩阵:

row.name   species1  species2 species3 species4 .... species50
sample 1      1         6        156      4              1
sample 2      0        20        34       5              1
sample 3      3         7        23       0              7
....
sample 10     3        15        9        7              6
这些是未经统计的数字

我正试图编写一种方法(但毫无结果),通过这种方法,我可以将样本/行中出现的物种数量超过10%的物种限制在9%。也就是说,在这个(虚构的)例子中,样本1/规格3似乎需要封顶

我希望数据保留为/恢复为原始计数。这在R中是否可能

我知道在
vegan
或等同于标准化/标准化数据中的生态变化,但它们不是我想要的

我希望这是有道理的。如果没有,我可以再解释一遍。非常感谢您提供的任何帮助,对于R

来说还是相当新的,我将使用
sweep()
,但指定
pmin
作为函数,以便 取10%和实际值中的较小值:

M <- read.table(header=TRUE, row.names = 'row.name', 
text='row.name   species1  species2 species3 species4  species50
sample_1      1         6        156      4              1
sample_2      0        20        34       5              1
sample_3      3         7        23       0              7
sample_10     3        15        9        7              6') 

M <- as.matrix(M)

sweep(M, 1, rowSums(M) %/% 10, pmin)

M您的意思是矩阵中任何大于10的值都应该替换为9吗?重新阅读后,可能不会。但是百分之十是什么?行和数?列总和?抱歉,行总和的10%如果有两个物种的数量大于10%,这可能会变得很棘手-一旦你改变一个物种的数量,你可能会发现另一个物种的数量上升到包含>=行总和的10%,因为行总和是动态的,那么我认为你正在进入优化领域。替换的计数必须精确等于9%吗?是的,这是我的想法。我想知道R能否动态地响应/平衡这一点。它们不必精确地等于9%,但我将为标签添加优化!谢谢,我来查一下,哪个包裹被收进来了@Geoff返回可能调整过的原始值对我来说是一个真正的挑战。你的方法很聪明!FUN中的扫描(数据,1,行和(数据)%/%10,pmin)错误(x,aperm(数组(STATS,dims[perm]),顺序(perm)),…):(列表)对象无法强制键入“double”我收到此错误吗?(很抱歉,我无法确定如何使其看起来像r脚本)您不能使用data.frame,您必须先转换为矩阵。哦,好的(忽略下面的答案!!)