R Studio:具有条件逻辑的平均多列

时间:2016-03-02 17:51:16

标签: r conditional average subset mean

在R studio中,如何为其中一列获得一个带有条件逻辑的多列平均值?例如,我有一个名为' test'的数据集。以下5列。我想获得A,B和C列的一个平均值。但是我只想在C列中的单元格中,如果Flag等于1,则平均包含C列。

    ID<-c(1,2,3)
    A<-c(1,0,NA)
    B<-c(0,NA,1)
    C<-c(1,1,1)
    Flag<-c(0,1,1)
   test<-as.data.frame(cbind(ID,A,B,C,Flag))

[click here to see my data set]

所以我想要:平均=(A列中的所有单元格+ B列中的所有单元格+仅C列中单元格列中的单元格等于1的单元格)

1 个答案:

答案 0 :(得分:1)

我们class NavButton : public QToolButton() { Q_OBJECT public: NavButton(int buttonid, bool buttonState = 0, QWidget *parent = 0) : QToolButton( parent ), id(buttonid), state(buttonState) { //setMouseTracking(true); setAttribute(Qt::WA_Hover); } ~NavButton(){} void setActiveIcon(QIcon active) {aIcon = active;} void setInActiveIcon(QIcon inactive) {iIcon = inactive;} void setHoverIcon(QIcon hover) {hIcon = hover;} int getId() {return id;} bool getState() {return state;} void setId(int buttonId) {id = buttonId;} void setState(int buttonState) { state = buttonState; QIcon icon = (state) ? aIcon : iIcon; setIcon(icon); } protected: void mousePressEvent(QMouseEvent *) {/*repaint();*/} void mouseReleaseEvent(QMouseEvent *) {emit stateChanged(id, state);} void hoverEnter(QHoverEvent *) {setIcon(QIcon(hIcon));} void hoverLeave(QHoverEvent *) { QIcon icon = (state) ? aIcon : iIcon; setIcon(icon); } bool event(QEvent *event) { switch(event->type()) { case QEvent::MouseButtonPress: mousePressEvent((QMouseEvent*)(event)); break; case QEvent::MouseButtonRelease: mouseReleaseEvent((QMouseEvent*)(event)); //event->ignore(); // Makes no difference //return false; // Makes no difference break; case QEvent::HoverEnter: hoverEnter((QHoverEvent*)(event)); break; case QEvent::HoverLeave: hoverLeave((QHoverEvent*)(event)); break; default: break; } return QWidget::event(event); } signals: void stateChanged(int id, bool state); private: int id; bool state; QIcon aIcon; QIcon iIcon; QIcon hIcon; }; 数据集包含列的子集(&#34; A&#34;,&#34; B&#34;),与&#34; C&#34;的值相连接。对应于&#34; Flag&#34;为1并得到unlist

mean