0

We were trying to write the results from a for loop. We tried to use write.table, as.data.frame and other solutions, but with no success. We expect to have a data frame.

Currently we have only the loop, that shows year and values from a matrix which are bigger than 50. Looks like that:

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

Note: We don't do programming a lot, so it's hard to use other solutions from the questions that already beed asked.

Jaap
  • 77,147
  • 31
  • 174
  • 185
Julia_S
  • 19
  • 1
  • 2
    [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269) – zx8754 Dec 21 '15 at 12:52

2 Answers2

2

Try to save each element to the vector, like here:

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)
Marta
  • 2,864
  • 2
  • 15
  • 34
0

If you just want to visually inspect a subset of your matrix, you can just print out a filtered subset:

# 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
Zelazny7
  • 38,056
  • 17
  • 66
  • 79