Ph.D.Student
Ph.D.Student

Reputation: 726

Create object with R

 num [1:300, 1] 0.7 -1 0.6 0.9 0.5 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:300] "G1" "G2" "G3" "G4" ...
  ..$ : NULL

How can I create an object with this structure using R?

Upvotes: 0

Views: 86

Answers (1)

kath
kath

Reputation: 7724

I don't know what exact numbers you want to have, but this is a matrix with rownames.

set.seed(1)
x <- matrix(rnorm(300), ncol = 1)
dimnames(x) <- list(paste0("G", 1:300), NULL)

str(x)
# num [1:300, 1] -0.626 0.184 -0.836 1.595 0.33 ...
# - attr(*, "dimnames")=List of 2
# ..$ : chr [1:300] "G1" "G2" "G3" "G4" ...
# ..$ : chr NULL

The set.seed(1) ensures you get the same result as there are random numbers involved (rnorm(300))

Upvotes: 2

Related Questions