ravi
ravi

Reputation: 1088

Without for loop generate t distribution data

I want to generate t distribution data using quantile function with df (degrees of freedom) vector and probability vector p.

p <- c(0.01, 0.001, 0.0001)
dof <- c(seq(1, 5, by = 1))

I can use for loop as below to do the job.

for (i in dof) {(qt(1-p, df=i))}

Without for loop, i can use lapply function but it gives output as a list.

val <- lapply(dof, function(d) qt(1-p, df=d))

How can I avoid for loop and get the output as data.frame.

Edit:

Using sapply result is correct but transpose form:

val <- sapply(dof, function(d) qt(1-p, df=d))
           [,1]      [,2]      [,3]      [,4]     [,5]
[1,]   31.82052  6.964557  4.540703  3.746947 3.364930
[2,]  318.30884 22.327125 10.214532  7.173182 5.893430
[3,] 3183.09876 70.700071 22.203742 13.033672 9.677566

Upvotes: 0

Views: 56

Answers (1)

Kactus
Kactus

Reputation: 122

would this work?

val <- sapply(dof, function(d) qt(1-p, df=d))

Upvotes: 1

Related Questions