我正在处理数据框ds
和此代码:
ds[,-1] = apply(ds[,-1],2,function(x){x/sum((x))})
此功能应该贯穿每列中的每个单元格。然后将每个单元除以列的总和。但是,运行此函数后,一些列填充了NaN,我不知道为什么。我如何弄清楚为什么有NaNs?有没有办法改变我的功能所以我没有得到NaN?
df
看起来像这样。
sample a b c
x 2 1 2
x1 3 0 45
运行str:
> str(df)
'data.frame': 99322 obs. of 257 variables:
$ sample : int 100021 1000032 100013 10001134 100014568 100014566 104600161 100017 1000188 10002 ...
$ FT579627: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT579617: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT579618: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578292: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578294: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578295: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578296: num NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN ...
$ FT578297: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578321: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578322: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578323: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578324: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578325: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578326: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578327: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578329: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578330: num 0 0 0 0 0 0 0 0 0 0 ...
$ FT578405: num 0 0 0 0 0 0 0 0 0 0 ...
[list output truncated]
答案 0 :(得分:1)
或许删除NA?
ds[,-1] = apply(ds[,-1],2,function(x){x/sum((x, na.rm=TRUE))})