Wiebke Kn
Wiebke Kn

Reputation: 13

R put for loop into vector

I would like to put the output of the following for loop into a single vector.

test=c("A","B","C","D")
for(i in 1:3)
 {e=runif(1,5,10);
  f=round(e);
  g=sample(test,f,TRUE);
  h=paste(g,collapse = "");
    print(h)}

Output:

[1] "BDCCABD"
[1] "DDBAADBBAA"
[1] "DACCAB"

I would like to get a vector like:

i=c("BDCCABD","DDBAADBBAA","DACCAB")

Thank you for your help

Upvotes: 0

Views: 168

Answers (6)

Rui Barradas
Rui Barradas

Reputation: 76402

Just a slight adaptation of your code will do it.

set.seed(8632)    # make the results reproducible

i <- sapply(1:3, function(x){
    e = runif(1, 5, 10)
    f = round(e)
    g = sample(test, f, TRUE)
    h = paste(g, collapse = "")
    print(h)
    h
})

i
#[1] "CACDAABCC" "ADDAACA"   "ACCDAACAB"

Do you really need to print(h)?

EDIT.
I've just tested it and the following simplification gives exactly the same result.

set.seed(8632)    # make the results reproducible

j <- sapply(1:3, function(x){
    f <- sample(5:10, 1)        # this is equivalent to your original code
    g = sample(test, f, TRUE)
    h = paste(g, collapse = "")
    print(h)
    h
})

j
#[1] "CACDAABCC" "ADDAACA"   "ACCDAACAB"

identical(i, j)
#[1] TRUE

Upvotes: 1

bobolafrite
bobolafrite

Reputation: 100

You should take a look at vectors in R

You need to initialize an empty vector, let's call it test_vector

test_vector = c()
test=c("A","B","C","D")

for(i in 1:3)
    {e=runif(1,5,10);
    f=round(e);
    g=sample(test,f,TRUE);
    h=paste(g,collapse = "");
    print(h)
    test_vector <- c(test_vector,h)
    }

Note that you could apply a function to your test vector directly without using a for loop.

Upvotes: 0

FAMG
FAMG

Reputation: 455

Not the most elegant way especially if you have lots of iterations but it works:

test=c("A","B","C","D")
k = NA
for(i in 1:3)
{e=runif(1,5,10)
f=round(e)
g=sample(test,f,TRUE)
h=paste(g,collapse = "")
k = append(k,h)
print(h)}

k <- na.omit(k)

Upvotes: 0

BENY
BENY

Reputation: 323226

You mention vector, then let us using vector

V=vector()
test=c("A","B","C","D")
for(i in 1:3)
{e=runif(1,5,10);
f=round(e);
g=sample(test,f,TRUE);
h=paste(g,collapse = "");
V[i]=h}

V
[1] "BCCAD"     "CCDCACBAD" "ADCDBCBCC"
V[1]
[1] "BCCAD"

Upvotes: 0

f.lechleitner
f.lechleitner

Reputation: 3812

something like this?

j <- character()

test = c("A", "B", "C", "D")
for (i in 1:3) {
  e = runif(1, 5, 10)
  f = round(e)
  g = sample(test, f, TRUE)
  h = paste(g, collapse = "")

  j <- c(j, h)
}

print(j)

> print(j)
[1] "DDDBADBCD" "ABBCBCC"   "BBCAA"

EDIT: Even simpler

test = c("A", "B", "C", "D")
for (i in 1:3) {
  e = runif(1, 5, 10)
  f = round(e)
  g = sample(test, f, TRUE)
  h[i] = paste(g, collapse = "")
  }

> print(h)
[1] "DBDADDD"  "AABDA"    "CDBDDABC"

Upvotes: 0

Henry Navarro
Henry Navarro

Reputation: 953

I think something like this:

test=c("A","B","C","D")
h_final<-0
for(i in 1:3){e=runif(1,5,10);
f=round(e);
g=sample(test,f,TRUE);
h=paste(g,collapse = "");
h_final[i]<-h
if(i==3){print(h_final)}
}

Upvotes: 0

Related Questions