In R: how to group the results of a loop in an rbind function?

0
Since I have a dataframe called page whose number of rows changes regularly, I wrote this code so that it fits the number of rows that my page$id has and I wrote this while so that it will generate a "g_com[numero]" for each of these lines.

meu page$id contém:
[1] "1793398447353793"
[2] "1792916894068615"
[3] "1792013524158952"
[4] "1791520780874893"
continua...

This is the script I'm running:

binds    <- as.data.frame(page$id)
contador <- 1

while (contador <= length(page$id)) { 
  binds[contador] <- paste0 ("g_com", contador)
  contador        <- contador + 1
}

b <- binds [1,]

It is returning a table with 17 columns and a row in each:

> print(binds)
page$id     V2     V3     V4     V5
1       g_com1, g_com2, g_com3, g_com4, g_com5,

However, I need to automatically group all these "g_com" into a sentence, so that it runs out of the comma after the last element and can be displayed on a single line, not columns, I need to run a rbind on them, so I need it to return something like

g_com1, g_com2, g_com3, ..., g_com17

If someone can help me, I appreciate it.

    
asked by anonymous 09.05.2017 / 07:03

1 answer

1

I generated some random data because I do not have access to the originals. See if the paste(unlist(b[2:length(b)]), collapse=", ") command at the end of the code helps.

page <- data.frame(id=rpois(17, lambda=100000))

binds    <- as.data.frame(page$id)
contador <- 1

while (contador <= length(page$id)) { 
  binds[contador] <- paste0("g_com", contador)
  contador        <- contador + 1
}

b <- binds [1,]

print(b)
  page$id     V2     V3     V4     V5     V6     V7     V8     V9     V10
1  g_com1 g_com2 g_com3 g_com4 g_com5 g_com6 g_com7 g_com8 g_com9 g_com10
      V11     V12     V13     V14     V15     V16     V17
1 g_com11 g_com12 g_com13 g_com14 g_com15 g_com16 g_com17

paste(unlist(b[2:length(b)]), collapse=", ")
[1] "g_com2, g_com3, g_com4, g_com5, g_com6, g_com7, g_com8, g_com9, g_com10, 
g_com11, g_com12, g_com13, g_com14, g_com15, g_com16, g_com17"

I can not see how the supplied code generated the original data with commas, since the code is not reproducible.

    
10.05.2017 / 02:13