0
votes

I have a vector of numbers:

> n <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

and a vector of powers:

> p <- c(1, 2, 3, 4, 5)

I would like to generate a data frame with 10 rows and 5 columns, whose rows are labeled by the members of the vector n, in order, and whose columns are labeled by the members of the vector p, in order, and such that the content of the cell that lies at the i'th row and the j'th column will be the number that labels the row to the power of the number that labels the column. For instance, the content of the cell at the intersection of the 3rd row with the 2nd column will be 9, because 9 = 3^2.

I would additionally like to know how to generate a similarly structured matrix.

Thank you.

1

1 Answers

1
votes

May be you can try

m1 <- outer(n, p, FUN='^')
dimnames(m1) <- list(n, p)
as.data.frame(m1)