我自己创建的函数:RemoveNA运行一半

时间:2019-01-23 08:47:45

标签: r

这是我的代码:

 class App extends React.Component {
      constructor(props){
        super(props);
        this.state = {
        data: [],
        library:null,
        perPage: 20,
        currentPage: 1,
        maxPage: null,
        filter: ""
        };
       }
   componentDidMount() {
   fetch('/json.bc')
  // Here I want to get the length of my respose 
  .then(response => response.text())
  .then(text => {
  var Maindata = JSON.parse(text.replace(/\'/g, '"'))
   this.setState(state => ({
   ...state,
   data: Maindata
     }), () => {
     this.reorganiseLibrary()
    })
   }).catch(error => console.error(error))
}
  reorganiseLibrary = () => {
  const { filter, perPage , data } = this.state;
  let library = data;
    if (filter !== "") {
      library = library.filter(item =>
      item.hotelinfo.hotelsearch.realname.toLowerCase().includes(filter)
   );
 }

 library = _.chunk(library, perPage);
   this.setState({
   library,
   currentPage: 1,
   maxPage: library.length === 0 ? 1 : library.length
 });
};
// Previous Page
  previousPage = () => 
  this.setState(prevState => ({
  currentPage: prevState.currentPage - 1
}));
// Next Page
  nextPage = () =>
   this.setState(prevState => ({
    currentPage: prevState.currentPage + 1
  }));

// handle filter
  handleFilter = evt =>
   this.setState(
     {
     filter: evt.target.value.toLowerCase()
   },
   () => {
     this.reorganiseLibrary();
   }
  );
 // handle per page
  handlePerPage = (evt) =>
   this.setState({
   perPage: evt.target.value 
}, () => this.reorganiseLibrary());

 // handle render of library
     renderLibrary = () => {
      const { library, currentPage } = this.state;
       if (!library || (library && library.length === 0)) {
        return <div class="tltnodata">no result!</div>;
    }
    return library[currentPage - 1].map((item, i) => (
       <input type="hidden" value={item.hotelinfo.hotelsearch.hotelid} name="hotelid"/>
   ));
 };

render() {
const { library, currentPage, perPage, maxPage } = this.state;
return (
  <div className="Main-wrapper">
    <div class="filter_hotelname"><input value={this.state.filter} onChange={this.handleFilter} class="hotelName" /></div>
      <div class="countHotel"> <span class="numbersearch"></span> // Here I want two show the count of items </div>
      <div className="wrapper-data">
          {this.renderLibrary()}
      </div>
      <div id="page-numbers">
          <div class="nexprev">
            {currentPage !== 1 && (
              <button onClick={this.previousPage}><span class="fa-backward"></span></button>
            )}
          </div>
          <div className="data_page-info">
          {this.state.currentPage} از {this.state.maxPage}
      </div>
          <div class="nexprev">
            {(currentPage < maxPage) && (
              <button onClick={this.nextPage}><span class="fa-forward"></span></button>
            )}
          </div>

      </div>

  </div>
    );
   }         
  }
 ReactDOM.render(<App/>, document.getElementById('Result')); 

当我运行功能 RemoveNA 时,它会给出: 您缺少的列是:“年龄”,列号是:6 ||||您缺少的列是:“ fare”,列号是:10 |||| 可以,但是下面的替换未正确完成,因为sum(is.na(df_titanic))之前和之后的总和最多为264

1 个答案:

答案 0 :(得分:2)

这是一种更直接的方法:

*

您的代码具有类型,您键入的是df1 <- data.frame(a= c(NA,1,NA,2), b = 1:4) df1[] <- lapply(df1, function(x) replace(x,is.na(x),mean(x,na.rm=TRUE))) df1 # a b # 1 1.5 1 # 2 1.0 2 # 3 1.5 3 # 4 2.0 4 而不是colsmiss

您的代码也不会返回任何内容(它会返回I的最后一个值),因此您对NA值的转换不会记录在任何地方。

您的更正功能:

colmiss