Tarkus
Tarkus

Reputation: 21

How do you write a matrix using a "for" loop in R?

So for data evaluation that I am doing at the moment I want to write a matrix using a "for" loop.

Let's say I have random numbers between 0 and 100:

E <- runif(100, 0, 100)

t <- 0 #start

for(t in 0:90) {
   D <- length(E[E >= t, E < (t + 10)])
   t = t + 10
}

So what I want to do is write "D" into a matrix at each iteration with "t" in one column and "D" in the other.

I've heard that you should avoid loops in R, but I don't know an alternative.

Upvotes: 2

Views: 154

Answers (2)

Mirek Długosz
Mirek Długosz

Reputation: 4275

It seems that you want to bin your variable into categories - this is exactly what cut does:

E <- runif(100, 0, 100)
table(cut(E, breaks = seq(0,100,10), right=FALSE))

#>  [0,10)  [10,20)  [20,30)  [30,40)  [40,50)  [50,60)  [60,70)  [70,80)  [80,90) 
#>      10       10        7       10        8       10       12       11       10 
#>[90,100) 
#>      12 

If you don't want to see categories labels, remove table call; if you want it in "tabular" format, wrap it in as.matrix.

Please note that if you are doing it for plotting purposes, then both hist and ggplot will do it automatically for you:

hist(E, breaks = seq(0,100,10))

hist output

library("ggplot2")
ggplot(data.frame(var=E), aes(x=var)) + geom_histogram(binwidth = 10)

ggplot output

Upvotes: 0

David Robinson
David Robinson

Reputation: 78590

Rather than using a loop, you can do this with sapply, which operates on each item in a sequence and stores the result in a vector, and then cbind to create the matrix:

E <- runif(100, 0, 100)
t <- seq(0, 90, 10)

D <- sapply(t, function(ti) {
  sum(E >= ti & E < (ti + 10))
})

cbind(t, D)
#>        t  D
#>  [1,]  0 11
#>  [2,] 10 12
#>  [3,] 20 14
#>  [4,] 30 11
#>  [5,] 40  9
#>  [6,] 50 12
#>  [7,] 60  7
#>  [8,] 70  7
#>  [9,] 80  6
#> [10,] 90 11

Note that I also used sum(E >= ti & E < (ti + 10)) rather than length(length(E[E >= ti & E < (ti + 10)])), as a slightly shorter way of finding the number of items in E that were greater than t but less than t + 10.

Upvotes: 5

Related Questions