【发布时间】:2020-05-14 01:02:55
【问题描述】:
我想用该列中大于零的最小值的一半替换数千行和列的数据框中的 0。我也不想包含前四列,因为它们是索引。
所以如果我从这样的事情开始:
index <- c("100p", "200p", 300p" 400p")
ratio <- c(5, 4, 3, 2)
gene <- c("gapdh", NA, NA,"actb"
species <- c("mouse", NA, NA, "rat")
a1 <- c(0,3,5,2)
b1 <- c(0, 0, 4, 6)
c1 <- c(1, 2, 3, 4)
as.data.frame(q) <- cbind(index, ratio, gene, species, a1, b1, c1)
index ratio gene species a1 b1 c1
100p 5 gapdh mouse 0 0 1
200p 4 NA NA 3 0 2
300p 3 NA NA 5 4 3
400p 2 actb rat 2 6 4
我希望得到这样的结果:
index ratio gene species a1 b1 c1
100p 5 gapdh mouse 1 2 1
200p 4 NA NA 3 2 2
300p 3 NA NA 5 4 3
400p 2 actb rat 2 6 4
我尝试了以下代码:
apply(q[-4], 2, function(x) "[<-"(x, x==0, min(x[x > 0]) / 2))
但我不断收到错误消息:Error in min(x[x > 0])/2 : non-numeric argument to binary operator
对此有任何帮助吗?非常感谢!
【问题讨论】:
标签: r function replace min zero