user3493789
user3493789

Reputation: 11

How can I construct a single table with append and loop?

I have a problem constructing a loop that gives me a single table by appending the outcome of the loop.

Now it is appending the columns (variables) horizontally instead of adding the rows vertically.

Maybe append is not the right function? Or is there a way to make it append vertically? Or maybe I only think that I am making a table but it is actually some other structure?

The solutions I found used rbind, but I did not figure out how to set up the loop with the rbind function.

for (i in 1:3) {
  users.humansofnewyork = append(users.humansofnewyork, getPost( (humansofnewyork$id[i]) , token, n = 500, comments = TRUE,likes = TRUE, n.likes=500, n.comments=500))
}

Thank you very much for the reply. Unfortunately, none of the solutions worked.

That's the full code:

#start the libaries
library(Rfacebook)
library(Rook)
library(igraph)

#browse to facebook and ask for token
browseURL("https://developers.facebook.com/tools/explorer")

token <- "...copy and paste token"

#get Facebook fanpage "humansofnewyork" with post id
humansofnewyork <- getPage("humansofnewyork", token, n=500)

users.humansofnewyork = c()

for (i in 1:3) {
  users.humansofnewyork = append(users.humansofnewyork, getPost( (humansofnewyork$id[i]) , token, n = 500, comments = TRUE,likes = TRUE, n.likes=500, n.comments=500))
}

Upvotes: 0

Views: 1066

Answers (3)

AMBarbosa
AMBarbosa

Reputation: 270

If the results of getPost have the same elements as the columns in users.humansofnewyork, this should work:

for (i in 1:3) {
  users.humansofnewyork[nrow(users.humansofnewyork) + 1, ] = getPost( (humansofnewyork$id[i]) , token, n = 500, comments = TRUE,likes = TRUE, n.likes=500, n.comments=500)
}

Or, using rbind,

for (i in 1:3) {
  users.humansofnewyork = rbind(users.humansofnewyork, getPost( (humansofnewyork$id[i]) , token, n = 500, comments = TRUE,likes = TRUE, n.likes=500, n.comments=500))
}

But if any of the columns in users.humansofnewyork is a factor and any of the new data includes new levels, you'd have to add the new factor levels first, or convert those columns to character.

Hope this helps. It would help us if you provided a reproducible example.

Upvotes: 0

Andriy T.
Andriy T.

Reputation: 2030

For example you have your data:

data <- data.frame ("x" = "a", "y" = 1000)
data$x <- as.character (data$x)
data
  x    y
1 a 1000

And you want to append new rows with a new values with a loop

for (i in 1:3) {  
  data <- rbind (data, c (paste0 ("Obs_",i), 10^i))  
}

So it wil gives you:

data
      x    y
1     a 1000
2 Obs_1   10
3 Obs_2  100
4 Obs_3 1000

You just have to care an order you introduce new values in c()

Upvotes: 0

Carl Witthoft
Carl Witthoft

Reputation: 21502

append is for vectors. You should use cbind, the column-wise brother of rbind. (I copied your code; no promises of success if getPost doesn't return a vector of the same length in each call)

for (i in 1:3) {
  users.humansofnewyork = cbind(users.humansofnewyork, getPost( (humansofnewyork$id[i]) , token, n = 500, comments = TRUE,likes = TRUE, n.likes=500, n.comments=500))
}

Upvotes: 1

Related Questions