Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用append和loop构造单个表?_R_Loops_Append_Rbind - Fatal编程技术网

R 如何使用append和loop构造单个表?

R 如何使用append和loop构造单个表?,r,loops,append,rbind,R,Loops,Append,Rbind,我在构造一个循环时遇到了一个问题,该循环通过附加循环的结果来为我提供一个表 现在,它水平地追加列(变量),而不是垂直地添加行 也许append不是正确的函数?或者有没有办法让它垂直附加? 或者也许我只是认为我在做一张桌子,但它实际上是另一种结构 我找到的解决方案使用了rbind,但我不知道如何使用rbind函数设置循环 for (i in 1:3) { users.humansofnewyork = append(users.humansofnewyork, getPost( (humans

我在构造一个循环时遇到了一个问题,该循环通过附加循环的结果来为我提供一个表

现在,它水平地追加列(变量),而不是垂直地添加行

也许append不是正确的函数?或者有没有办法让它垂直附加? 或者也许我只是认为我在做一张桌子,但它实际上是另一种结构

我找到的解决方案使用了rbind,但我不知道如何使用rbind函数设置循环

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))
}

非常感谢您的回复。不幸的是,所有的解决方案都不起作用

这是完整的代码:

#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))
}
#启动库
图书馆(Rfacebook)
图书馆(Rook)
图书馆(igraph)
#浏览facebook并索取代币
布劳瑟尔(”https://developers.facebook.com/tools/explorer")

标记
append
用于向量。您应该使用
cbind
,它是
rbind
的列兄弟。(我复制了您的代码;如果
getPost
在每次调用中不返回相同长度的向量,则不会保证成功)


例如,您有您的数据:

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

如果
getPost
的结果与
users中的列具有相同的元素,则只需注意在
c()中引入新值的顺序。纽约的humansofnewyork
,这应该可以:

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)
}
或者,使用
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))
}
但是如果
users.humansofnewyork
中的任何列是
因子
,并且任何新数据都包含新的级别,则必须首先添加新的因子级别,或者将这些列转换为
字符

希望这有帮助。如果你能提供一个可复制的例子,这将对我们有所帮助

data
      x    y
1     a 1000
2 Obs_1   10
3 Obs_2  100
4 Obs_3 1000
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)
}
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))
}