まず取得データセットの行数と列数:
response = 0;
for(ir in 1:nr) { // cycle through rows
for(ic in 1:nc) { // cycle through columns
response = response + ifelse(newframe[ir, ic]<0, 1, 0);
}
}
response
変数は、データセットの負の値の数が含まれています:行とcolums通じ
nr = NROW(newframe); # you can use the lowercase variant as well or the dim command
nc = NCOL(newframe);
、その後サイクル。
サイクルは、のために、それぞれの文のように動作し、アカウントにforループの外側を取ることができます:ir
はコマンド1:nr
によって生成されたシリーズの全ての値をとります(つまり1
、2
、... nr
です) 。
response = 0;
for(v in unlist(newframe)) { # you need to un-list the dataframe otherwise you would cycle through the columns...
response = response + ifelse(v<0, 1, 0);
}
:
別の例では、出力
> a
b
c
これはあなたがループシングルを使用して同じ結果をachiveことができることを意味し得るでしょう
x = c("a", "b", "c");
for(v in x) {
cat(v, "\n");
}
ですまた、あなたが好きなときはforループからwhileループにいつでも切り替えることができます:
response = 0;
ir = 1;
while(ir<=nr) { // cycle through rows
ic = 1;
while(ic<=nc) { // cycle through columns
response = response + ifelse(newframe[ir, ic]<0, 1, 0);
ic = ic + 1;
}
ir = ir + 1;
}
私は私はあなたの質問:)