首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将函数应用于tibble的每一种情况

将函数应用于tibble的每一种情况
EN

Stack Overflow用户
提问于 2020-04-23 15:25:57
回答 1查看 34关注 0票数 0

我的第二次参与是在Stackoverflow中。

我有一个名为bw_test的函数,其中有几个参数如下:

代码语言:javascript
复制
  bw_test <- function(localip, remoteip, localspeed, remotespeed , duracion =30,direction ="both"){

comando <- str_c("ssh usuario@", localip ," /tool bandwidth-test direction=", direction," remote-tx-speed=",remotespeed,"M local-tx-speed=",localspeed,"M protocol=udp user=usuario password=mipasso  duration=",duracion," ",remoteip)

  resultado <- system(comando,intern = T,ignore.stderr  = T)

# resultado pull from a ssh server a vector like this:
# head(resultado)
#[1] "                status: connecting\r" "            tx-current: #0bps\r"       "  tx-10-second-average: 0bps\r"      
#[4] "      tx-total-average: 0bps\r"       "            rx-current: #0bps\r"       "  rx-10-second-average: 0bps\r"       

  resultado %<>%
    replace("\r","") %>%
    tail(17) %>% 
    trimws("both") %>%
    as_tibble %>%
    mutate(local=localip, remote=remoteip) %>%
    separate(value,sep=":", into=c("parametro","valor")) %>%
    head(15) 


  resultado$valor %<>%
    trimws() %>%
    str_replace("Mbps","") %>% str_replace("%","") %>% str_replace("s","")

  resultado  %<>% 
  spread(parametro,valor)  
  resultado %<>%
    mutate(`tx-percentaje`=as.numeric(resultado$`tx-total-average`)/localspeed) %>%
    mutate(`rx-percentaje`=as.numeric(resultado$`rx-total-average`)/remotespeed) 

  return(resultado)


    }

此函数返回如下tibble:

代码语言:javascript
复制
A tibble: 1 x 19
  local remote `connection-cou… direction duration `local-cpu-load` `lost-packets` `random-data` `remote-cpu-loa…
  <chr> <chr>  <chr>            <chr>     <chr>    <chr>            <chr>          <chr>         <chr>           
1 192.… 192.1… 1                both      4        13               0              no            12              
# … with 10 more variables: `rx-10-second-average` <chr>, `rx-current` <chr>, `rx-size` <chr>,
#   `rx-total-average` <chr>, `tx-10-second-average` <chr>, `tx-current` <chr>, `tx-size` <chr>,
#   `tx-total-average` <chr>, `tx-percentaje` <dbl>, `rx-percentaje` <dbl>

所以,当我调用rbind中的函数时,得到了tibble上每次运行的结果:

代码语言:javascript
复制
rbind(bw_test("192.168.105.10" ,"192.168.105.18", 75,125),
      bw_test("192.168.133.11","192.168.133.9", 5 ,50),
      bw_test("192.168.254.251","192.168.254.250",  25,150))

我的结果是为了这个例子:

代码语言:javascript
复制
# A tibble: 3 x 19
  local remote `connection-cou… direction duration `local-cpu-load` `lost-packets` `random-data` `remote-cpu-loa…
  <chr> <chr>  <chr>            <chr>     <chr>    <chr>            <chr>          <chr>         <chr>           
1 192.… 192.1… 20               both      28       63               232            no            48              
2 192.… 192.1… 20               both      29       4                0              no            20              
3 192.… 192.1… 20               both      29       15               0              no            22              
# … with 10 more variables: `rx-10-second-average` <chr>, `rx-current` <chr>, `rx-size` <chr>,
#   `rx-total-average` <chr>, `tx-10-second-average` <chr>, `tx-current` <chr>, `tx-size` <chr>,
#   `tx-total-average` <chr>, `tx-percentaje` <dbl>, `rx-percentaje` <dbl>

我的问题是将该函数应用于tibble这样的情况。

代码语言:javascript
复制
aps <- tribble(
  ~name, ~ip, ~remoteip , ~bw_test, ~localspeed,~remotespeed,
  "backbone_border_core","192.168.253.1", "192.168.253.3", 1,200,200,
  "backbone_2_site2","192.168.254.251", "192.168.254.250", 1, 25,150
}

我试着使用地图,但是我得到了:

代码语言:javascript
复制
map(c(aps$ip,aps$remoteip,aps$localspeed,aps$remotespeed), bw_test)

el argumento "remotespeed" está ausente, sin valor por omisión 

我相信c(aps$ip,aps$remoteip,aps$localspeed,aps$remotespeed)首先提供所有aps$ip,然后是所有aps$remoteip,依此类推。

我用的是正确的策略?这是一种合适的方式

我哪里做错了?

如何对每一行应用函数以获取请求的tibble?

我将非常感谢你的帮助。

欢迎光临。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-23 16:21:12

尝试使用pmap_df

代码语言:javascript
复制
output <- purrr::pmap_df(list(aps$ip, aps$remoteip, aps$localspeed, 
                              aps$remotespeed), bw_test)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61381562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档