dpylr如何总结给定的多级分组

时间:2017-07-27 00:33:41

标签: r dplyr

我有一个像

这样的数据框
count  sample  type
1  DLF002     a
1  DLF002     a
1  DLF002     b
3  DLF001     a
3  DLF001     b
3  DLF001     b

并且想给出两个因素(样本和类型)的摘要,所以我希望得到如下结果

count  sample  type
2  DLF002     a
1  DLF002     b
3  DLF001     a
6  DLF001     b

我尝试了类似的事情,

group_by(df, sample) %>% summarize(sx = sum(count)),

但这并没有考虑"类型&#34 ;;我想知道如何使用dpylr

进行这两级分组

1 个答案:

答案 0 :(得分:1)

只需将多个分组变量添加到public class AdditionEasy extends AppCompatActivity { int countCNumAddE = 0; int countWNumAddE = 0; boolean hasAnswered; public static final String MY_PREFS_NAME = "MyPrefsFile"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addition); final TextView count = (TextView) findViewById(R.id.Count); final TextView count2 = (TextView) findViewById(R.id.Count2); Button homeButton = (Button) findViewById(R.id.homeButton); super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); final TextView textOne = (TextView) findViewById(R.id.textView); final TextView textTwo = (TextView) findViewById(R.id.textView2); final Button pushMe1 = (Button) findViewById(R.id.button1); final Button pushMe2 = (Button) findViewById(R.id.button2); final Button pushMe3 = (Button) findViewById(R.id.button3); final Button pushMe4 = (Button) findViewById(R.id.button4); final Button begin = (Button) findViewById(R.id.begin); begin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { hasAnswered = false; pushMe1.setEnabled(true); pushMe2.setEnabled(true); pushMe3.setEnabled(true); pushMe4.setEnabled(true); begin.setVisibility(View.INVISIBLE); pushMe1.setVisibility(View.VISIBLE); pushMe2.setVisibility(View.VISIBLE); pushMe3.setVisibility(View.VISIBLE); pushMe4.setVisibility(View.VISIBLE); pushMe1.setTextColor(Color.BLACK); pushMe2.setTextColor(Color.BLACK); pushMe3.setTextColor(Color.BLACK); pushMe4.setTextColor(Color.BLACK); pushMe1.setTextSize(20); pushMe2.setTextSize(20); pushMe3.setTextSize(20); pushMe4.setTextSize(20); textTwo.setText(""); String randGenChoice1 = ""; String randGenChoice2 = ""; String randGenChoice3 = ""; String randGenChoice4 = ""; String randText2 = ""; String randText3 = ""; Random RandomNum = new Random(); int randChoice1 = RandomNum.nextInt(40) + 1; int randChoice2 = RandomNum.nextInt(40) + 1; int randChoice3 = RandomNum.nextInt(40) + 1; int randChoice4 = RandomNum.nextInt(40) + 1; int rando2 = RandomNum.nextInt(20) + 1; int rando3 = RandomNum.nextInt(20) + 1; int pick = RandomNum.nextInt(4); randGenChoice1 = Integer.toString(randChoice1); randGenChoice2 = Integer.toString(randChoice2); randGenChoice3 = Integer.toString(randChoice3); randGenChoice4 = Integer.toString(randChoice4); randText2 = Integer.toString(rando2); randText3 = Integer.toString(rando3); int value1; int value2; value1 = Integer.parseInt(randText2); value2 = Integer.parseInt(randText3); final int value = value1 + value2; String line = randText2 + " + " + randText3; textOne.setText(line); final String answer; answer = Integer.toString(value); pushMe1.setText(randGenChoice1); pushMe2.setText(randGenChoice2); pushMe3.setText(randGenChoice3); pushMe4.setText(randGenChoice4); Button[] choice = {pushMe1, pushMe2, pushMe3, pushMe4}; Button display = choice[pick]; display.setText(answer); pushMe1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int buttonAnswer = Integer.parseInt(pushMe1.getText().toString()); if (buttonAnswer == value) { begin.setVisibility(View.VISIBLE); textTwo.setText("Correct!"); textTwo.setTextColor(Color.BLACK); pushMe1.setTextColor(Color.GREEN); pushMe1.setTextSize(30); if (hasAnswered != true) { String AdditionEasyRight = Integer.toString(++countCNumAddE); count.setText(AdditionEasyRight); hasAnswered = true; } begin.setText("New Question"); begin.setTextSize(20); pushMe2.setVisibility(View.INVISIBLE); pushMe3.setVisibility(View.INVISIBLE); pushMe4.setVisibility(View.INVISIBLE); pushMe1.setEnabled(false); pushMe2.setEnabled(false); pushMe3.setEnabled(false); pushMe4.setEnabled(false); }else{ textTwo.setText("Wrong!"); textTwo.setTextColor(Color.BLACK); pushMe1.setTextColor(Color.RED); pushMe1.setTextSize(30); if (hasAnswered != true) { String AdditionEasyWrong = Integer.toString(++countWNumAddE); count2.setText(AdditionEasyWrong); hasAnswered = true; } pushMe1.setEnabled(false); } } }); pushMe2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int buttonAnswer = Integer.parseInt(pushMe2.getText().toString()); if (buttonAnswer == value) { begin.setVisibility(View.VISIBLE); textTwo.setText("Correct!"); textTwo.setTextColor(Color.BLACK); pushMe2.setTextColor(Color.GREEN); pushMe2.setTextSize(30); if (hasAnswered != true) { String AdditionEasyRight = Integer.toString(++countCNumAddE); count.setText(AdditionEasyRight); hasAnswered = true; } begin.setText("New Question"); begin.setTextSize(20); pushMe1.setVisibility(View.INVISIBLE); pushMe3.setVisibility(View.INVISIBLE); pushMe4.setVisibility(View.INVISIBLE); pushMe1.setEnabled(false); pushMe2.setEnabled(false); pushMe3.setEnabled(false); pushMe4.setEnabled(false); }else{ textTwo.setText("Wrong!"); textTwo.setTextColor(Color.BLACK); pushMe2.setTextColor(Color.RED); pushMe2.setTextSize(30); if (hasAnswered != true) { String AdditionEasyWrong = Integer.toString(++countWNumAddE); count2.setText(AdditionEasyWrong); hasAnswered = true; } pushMe2.setEnabled(false); } } }); pushMe3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int buttonAnswer = Integer.parseInt(pushMe3.getText().toString()); if (buttonAnswer == value) { begin.setVisibility(View.VISIBLE); textTwo.setText("Correct!"); textTwo.setTextColor(Color.BLACK); pushMe3.setTextColor(Color.GREEN); pushMe3.setTextSize(30); if (hasAnswered != true) { String AdditionEasyRight = Integer.toString(++countCNumAddE); count.setText(AdditionEasyRight); hasAnswered = true; } begin.setText("New Question"); begin.setTextSize(20); pushMe1.setVisibility(View.INVISIBLE); pushMe2.setVisibility(View.INVISIBLE); pushMe4.setVisibility(View.INVISIBLE); pushMe1.setEnabled(false); pushMe2.setEnabled(false); pushMe3.setEnabled(false); pushMe4.setEnabled(false); }else{ textTwo.setText("Wrong!"); textTwo.setTextColor(Color.BLACK); pushMe3.setTextColor(Color.RED); pushMe3.setTextSize(30); if (hasAnswered != true) { String AdditionEasyWrong = Integer.toString(++countWNumAddE); count2.setText(AdditionEasyWrong); hasAnswered = true; } pushMe3.setEnabled(false); } } }); pushMe4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int buttonAnswer = Integer.parseInt(pushMe4.getText().toString()); if (buttonAnswer == value) { begin.setVisibility(View.VISIBLE); textTwo.setText("Correct!"); textTwo.setTextColor(Color.BLACK); pushMe4.setTextColor(Color.GREEN); pushMe4.setTextSize(30); if (hasAnswered != true) { String AdditionEasyRight = Integer.toString(++countCNumAddE); count.setText(AdditionEasyRight); hasAnswered = true; } begin.setText("New Question"); begin.setTextSize(20); pushMe1.setVisibility(View.INVISIBLE); pushMe2.setVisibility(View.INVISIBLE); pushMe3.setVisibility(View.INVISIBLE); pushMe1.setEnabled(false); pushMe2.setEnabled(false); pushMe3.setEnabled(false); pushMe4.setEnabled(false); }else{ textTwo.setText("Wrong!"); textTwo.setTextColor(Color.BLACK); pushMe4.setTextColor(Color.RED); pushMe4.setTextSize(30); if (hasAnswered != true) { String AdditionEasyWrong = Integer.toString(++countWNumAddE); count2.setText(AdditionEasyWrong); hasAnswered = true; } pushMe4.setEnabled(false); } } }); } }); homeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent homepage = new Intent(AdditionEasy.this , Menu.class); startActivity(homepage); } }); }

group_by