2015-12-21 55 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