R均值函数的问题

时间:2019-02-19 06:33:25

标签: r

R返回NA时,mean函数遇到问题。

这是我的数据(is.numeric返回TRUE):

[1] 0.0148 0.0033 0.0021 0.0018 0.0013     NA     NA     NA 0.0075 0.0068 0.0059 0.0037 0.0033 0.0025 0.0019 0.0018 0.0072 0.0064 0.0044

[20] 0.0027 0.0024 0.0024 0.0012 0.0004

总结结果:

summary(Display_df$CTR)
Min.  1st Qu.   Median     Mean  3rd Qu.     Max.     NA's 
0.000400 0.001900 0.002700 0.003991 0.005900 0.014800        3 

平均值的结果:

mean(Display_df$CTR, rm.na = T)
[1] NA

有什么想法吗?我以前从未遇到过这个问题,而且看起来很简单。

1 个答案:

答案 0 :(得分:0)

您误会了一个论点。重现 //here is the difference, when you use function, function takes arguements as it is. const UpdateIcons = (products) => { // if you use this as component props will be like products props will return the products object again, here you have to use {products} destructuring or products.propducts["One"] //like this const UpdateIcons = ({products}) => { if((products["One"] === "A") && (products["Two"] === "B") && (products["Three"] === "C") ) { return <p>Conditional</p> } return <p>Show Always</p> } class MyIssue extends Component { render() { const { products } = this.props; return ( <div> {UpdateIcons(products)} {/*passing arguements here products will be {"One": "A", "Two": "B"}*/} <UpdateIcons products={products} /> {/* passing props , props will be same as products */} </div>) } } class App extends Component { constructor() { super(); this.state = { products: { "One": "A", "Two": "B", "Three": "C" } }; } render() { return <MyIssue products={this.state.products} /> } } 的方法如下:

NA

这将返回NA,因为R不对NA进行计算。例如test<-c(1,2,3,NA,NA) mean(test,rm.na=T) 将返回1+NA。正确的参数如下所示

NA

mean(test,na.rm = T) 而不是na.rm