2015-12-21 56 views
0

我們試圖從for循環寫入結果。我們嘗試使用write.table,as.data.frame和其他解決方案,但沒有成功。我們期望有一個數據框。編寫for循環的結果

目前我們只有循環,顯示年,從矩陣比50大值貌似是:

for (i in 1:nrow(dobowe1)) { 
    if(dobowe1[i,4]>50) { 
    cat(dobowe1[i,"rok"],dobowe1[i,4], "\n") 
    } 
} 

注:我們不這樣做編程很多,所以很難從已經提出的問題中使用其他解決方案。

+2

[如何製作一個很好的R可重現的例子?](http://stackoverflow.com/questions/5963269) – zx8754

回答

2

嘗試每個元素保存到向量,喜歡這裏:

tabela <- numeric(nrow(dobowe1)) 
for (i in 1:nrow(dobowe1)) { 
    if(dobowe1[i,4]>50) { 
    tabela[i] <- paste(dobowe1[i,"rok"],dobowe1[i,4]) 
    } 
} 
as.data.frame(tabela) 
0

如果你只是想直觀地檢查你的矩陣的一個子集,你可以打印出一個過濾的子集:

# create the filter: 

> f <- dobowe1[,4] > 50 

# use the filter to subset (index) your data.frame: 

> dobowe1[f,c("rok", whatever-4th-var-is-called)] 

This will automatically print it out. Or you can write it to a file with ?write.table