먼저 얻을 데이터 집합의 행과 열 수 :
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);
다음주기 .
사이클이를위한 각 문장처럼 작동
, 외부가 루프를 고려 할 수 있습니다 : ir
명령 1:nr
에 의해 생성 된 시리즈의 모든 값 (즉 ..., nr
2
1
입니다) 소요됩니다 .
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);
}
:
또 다른 예는
x = c("a", "b", "c");
for(v in x) {
cat(v, "\n");
}
당신이 당신을 위해 루프 하나를 사용하여 동일한 결과를 achive 수 있음을 의미 출력
> a
b
c
를 얻을 수 있습니다입니다 원하는 경우 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;
}
나는 :)