带弹性包装的R中的for环 [英] for loop in R with the elastic package

查看:44
本文介绍了带弹性包装的R中的for环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有用户ID的userIds(类-整数)的向量:

I have a vector with user ID's userIds(class - integer):

873611  875908  876399  877630  878295  878794  880618  881646

和一个代码:

aggs <- '{
  "size": 10000, 
  "filter": {
    "term": {
      "user_id": "593586" #<- there will be all user ids
    }
  },
  "sort": [{
    "@timestamp": { "order": "asc" }
  }] 
}'
tablTogether = Search(index="fort", body=aggs, asdf = TRUE)

我需要创建一个包含所有用户ID的代码循环.

I need to create a loop of this code with all users id's.

类似这样的东西:

 for (int i = 0; i<userIds.lengths; i++)
{
aggs <- '{
"size": 10000, 
"filter": {
"term": {
  "user_id": "+userIds[i]+" #<- Is it right?
}
},
"sort": [{
"@timestamp": { "order": "asc" }
 }] 
 }'
 tablTogether = Search(index="fort", body=aggs, asdf = TRUE)
 }

如何将此代码翻译为R. Thx以帮助我!!!

How to translate this code to R. Thx for helping me!!!

推荐答案

我是 elastic 包的作者-希望我能为您提供帮助:/

i'm the author of the elastic package - so hopefully I can help :/

这是一种使用for循环的方法

Here's a way to do it with a for loop

aggs <- '{
  "size": 10000, 
  "filter": {
    "term": {
      "user_id": "%s"
    }
  },
  "sort": [{
    "@timestamp": { "order": "asc" }
  }] 
}'
out = list()
for (i in seq_along(users)) {
  out[[i]] <- Search(index = "fort", 
                     body = sprintf(aggs, users[i]), 
                     asdf = TRUE)
}

让我知道是否可行.

这篇关于带弹性包装的R中的for环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆