runjumpfly - 9 months ago 47

R Question

I want to create the following vector using a, b, c repeating each letter thrice:

`BB<-c("a","a","a","b","b","b","c","c","c")`

This is my code:

`Alphabet<-c("a","b","c")`

AA<-list()

for(i in 1:3){

AA[[i]]<-rep(Alphabet[i],each=3)

}

BB<-do.call(rbind,AA)

But I am getting a dataframe:

`dput(BB)`

structure(c("a", "b", "c", "a", "b", "c", "a", "b", "c"), .Dim = c(3L,

3L))

What I am doing wrong?

Answer Source

As Akrun mentioned we can use the same rep function create a vector which consists of letters a,b,c

```
A <- c("A","B","C")
```

Apply rep function for the same vector, use each as sub function

```
AA <- rep(A,each=3)
print(AA)
[1] "A" "A" "A" "B" "B" "B" "C" "C" "C"
```