我的数据框“ c1”
steps interval
<dbl> <int>
1 0 0
2 0 5
3 0 10
4 0 15
5 0 20
6 0 25
7 0 30
8 0 35
9 0 40
10 0 45
# ... with 278 more rows
PS: Sorry for providing only a small sample from data
我想通过为每50个间隔添加“ interval”列的行来创建新的数据帧'c11',以便相应地计算每50步(列步)的总和。
我的新数据框应该看起来像
steps interval
<dbl> <int>
1 0 0
2 0 50
3 0 100
4 10 150
5 0 200
6 6 250
7 20 300
8 0 350
9 230 400
10 0 450
我的代码
n=0
j=0
y <- nrow(c1)/50
class(n)
c11 <- data.frame(matrix(NA, nrow = y, ncol = 2))
class(c11)
for (i in 1:nrow(c1)) {
n <- n + c1[i,1]
if (i%%50==0)
{
c11[j,1]<- i
c11[j,2] <- n
j <- j+1
n = 0
}
}
有人会更正代码,因为它显示了错误的输出?
答案 0 :(得分:1)
尝试在package onsource.com.onsourceinspector.data.api;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class saludos extends AppCompatActivity {
private TextView monthsDisplay;
int i = 0;
private Button next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saludos);
monthsDisplay = (TextView) findViewById(R.id.espacioFrases);
next = (Button) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showMonths();
}
});
}
String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
String monthToDisplay = "";
public void showMonths() {
if (i > 11)
i = 0;
monthsDisplay.setText(months[i++]);
}
}
中使用cut
aggregate
为了更加明确,我们可以将步骤分开
with(c1, aggregate(list(steps = steps),
list(interval = cut(interval, c(seq(min(interval), max(interval), 50), Inf),
labels = seq(min(interval), max(interval), 50), include.lowest = TRUE)), sum))