本文介绍了循环遍历数据帧以获取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数据帧:

  v w  e  
1 d f  -- 
2 d a  -> 
3 f a  -> 
4 q a  -> 

并编写此函数:

convert_dag <- function(df,v,w,e) {
  z <- 'dag{'
  for (row in 1:nrow(df)) {
    if (df[row,'v'] == v && df[row,'w'] == w) {
      z <- paste(z,paste(df[row,'v'], e, df[row,'w']), collapse = '')
    } else {
      z <- paste(z,paste(df[row,'v'], df[row,'e'], df[row,'w']), collapse = '')
    }
  }
  z <- paste(z,'}')
  z
}

此函数将获取数据帧,并根据用户输入更新值,然后输出字符串。我的问题是,是否有更有效的方式使用tidyverse语法编写此代码?

示例:

> convert_dag(df,'d','f','->')
[1] "dag{ d -> f d -> a f -> a q -> a }"

推荐答案

当然可以。

library(tidyverse)
df = tribble(
  ~v,  ~w,  ~e,  
  "d", "f",  "--", 
  "d", "a",  "->", 
  "f", "a",  "->", 
  "q", "a",  "->")

convert_dag = function(df,V,W,E) {
  df %>% mutate(
    txt = ifelse(v==V & w==W, paste(V,E,W), paste(v,e,w))
  ) %>% pull(txt) %>% paste(.,collapse = " ") %>%  paste('dag{',.,'}')
}
convert_dag(df, "d", "f", "->")
#[1] "dag{ d -> f d -> a f -> a q -> a }"

更新%1

n = 40
tibble(
  id = 1:n,
  V = sample(c("d", "f", "q"), n, replace = TRUE),
  W = sample(c("a", "f"), n, replace = TRUE),
  e = sample(c("--", "->", "<-", "->>", "<<-"), n, replace = TRUE)
) %>% group_by(id) %>% 
  nest(data=V:e) %>% 
  mutate(con_dag = map(data, ~convert_dag(df, .x$V, .x$W, .x$e))) %>% 
  unnest(data:con_dag)

输出

# A tibble: 40 x 5
# Groups:   id [40]
      id V     W     e     con_dag                            
   <int> <chr> <chr> <chr> <chr>                              
 1     1 q     f     ->>   dag{ d -- f d -> a f -> a q -> a } 
 2     2 f     a     ->    dag{ d -- f d -> a f -> a q -> a } 
 3     3 f     f     --    dag{ d -- f d -> a f -> a q -> a } 
 4     4 f     a     ->    dag{ d -- f d -> a f -> a q -> a } 
 5     5 q     a     ->>   dag{ d -- f d -> a f -> a q ->> a }
 6     6 q     a     <<-   dag{ d -- f d -> a f -> a q <<- a }
 7     7 q     f     <<-   dag{ d -- f d -> a f -> a q -> a } 
 8     8 f     f     <-    dag{ d -- f d -> a f -> a q -> a } 
 9     9 d     a     ->    dag{ d -- f d -> a f -> a q -> a } 
10    10 q     a     ->    dag{ d -- f d -> a f -> a q -> a } 
# ... with 30 more rows
注意,由于tibble突变的特定构造,它必须包含一个分组变量。这里是变量id

更新2

tibble(
  id = 1:2,
  V = c('d','d'),
  W = c('f','a'),
  e = c('->','<-')
) %>% group_by(id) %>%
  nest(data=V:e) %>%
  mutate(con_dag = map(data, ~convert_dag(df, .x$V, .x$W, .x$e))) %>%
  unnest(data:con_dag)

输出

# A tibble: 2 x 5
# Groups:   id [2]
     id V     W     e     con_dag                           
  <int> <chr> <chr> <chr> <chr>                             
1     1 d     f     ->    dag{ d -> f d -> a f -> a q -> a }
2     2 d     a     <-    dag{ d -- f d <- a f -> a q -> a }

现在清楚了吗?

更新3

convert_dag2 = function(df,V,W,E) {
  df %>% mutate(
    test = v %in% V & w %in% W,
    txt = ifelse(test, paste(V,E,W), paste(v,e,w))
  ) %>% pull(txt) %>% paste(.,collapse = " ") %>%  paste('dag{',.,'}')
}
convert_dag2(df, c('d','d'), c('f','a'), c('->','<-'))
# [1] "dag{ d -> f d <- a f -> a q -> a }"

这篇关于循环遍历数据帧以获取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 08:10