如何将多列数据框合并为单个文本字符串,逐行捕获信息?

huangapple go评论65阅读模式
英文:

How do I collapse a multi-column data.frame into a single text string capturing information row by row?

问题

目标是获取一些情节数据,然后将最新的数据点放入标题中作为一个字符串。

我的示例数据是:

    df <- data.frame(state=c("买入", "卖出"), freq=c(0.102, .801))

看起来是这样的:

    > df
      state  freq
    1   买入 0.102
    2   卖出 0.801

我想要得到这样的字符串:

    mystring <- "买入 10.2%,卖出 80.1%"  

所以我想要第一行的状态1和频率1,然后第二行的状态2和频率2。

我尝试过使用unlist和paste来进行拼接,但还没有成功。除了循环遍历每一行之外,是否有更聪明的dplyr/tidyverse方法?
英文:

The goal is to take some plot data and then put the latest points in the caption as a string.

My toy data is:

df &lt;- data.frame (state=c(&quot;buy&quot;, &quot;sell&quot;), freq=c(0.102, .801))

so it looks like this:

&gt; df
  state  freq
1   buy 0.102
2  sell 0.801

I want to get the string:

mystring &lt;- &quot;buy 10.2%, sell 80.1%&quot;  

So I want first row state1 with freq1 then second row state2 with freq2.

I have tried unlisting and paste with collapse but no luck yet. Apart from looping over every row is there a smarter dplyr/tidyverse way?

答案1

得分: 3

"library(stringr)\n\nstr_glue_data(df, "{state} {freq*100}%") |>\nstr_flatten_comma()\n# [1] "buy 10.2%, sell 80.1%""

英文:
library(stringr)

str_glue_data(df, &quot;{state} {freq*100}%&quot;) |&gt;
  str_flatten_comma()
# [1] &quot;buy 10.2%, sell 80.1%&quot;

答案2

得分: 1

你可以首先粘贴每一行,然后最后合并它:

library(dplyr)
df <- data.frame(state=c("buy", "sell"), freq=c(0.102, .801))

df %>%
  mutate(together = paste0(state, " ", freq * 100, "%")) %>%
  pull(together) %>%
  paste0(collapse = ", ")

创建于2023年05月24日,由reprex包 (v1.0.0)创建

英文:

You can first paste every row and then collapse it in the end:

library(dplyr)
#&gt; 
#&gt; Attaching package: &#39;dplyr&#39;
#&gt; The following objects are masked from &#39;package:stats&#39;:
#&gt; 
#&gt;     filter, lag
#&gt; The following objects are masked from &#39;package:base&#39;:
#&gt; 
#&gt;     intersect, setdiff, setequal, union

df &lt;- data.frame (state=c(&quot;buy&quot;, &quot;sell&quot;), freq=c(0.102, .801))

df %&gt;% 
  mutate(together = paste0(state, &quot; &quot;, freq * 100, &quot;%&quot;)) %&gt;% 
  pull(together) %&gt;% 
  paste0(collapse = &quot;, &quot;)
#&gt; [1] &quot;buy 10.2%, sell 80.1%&quot;

<sup>Created on 2023-05-24 by the reprex package (v1.0.0)</sup>

答案3

得分: 1

我们可以使用 pmap_vec

library(dplyr)
library(purrr)

df %>% 
   mutate(freq = paste0(round(freq * 100, 1), "%")) %>% 
   pmap_vec(paste, sep = " ") %>% 
   paste(collapse = ", ")
[1] "buy 10.2%, sell 80.1%"
英文:

We could use pmap_vec:

library(dplyr)
library(purrr)

df %&gt;% 
   mutate(freq = paste0(round(freq * 100, 1), &quot;%&quot;)) %&gt;% 
   pmap_vec(paste, sep = &quot; &quot;) %&gt;% 
   paste(collapse = &quot;, &quot;)
[1] &quot;buy 10.2%, sell 80.1%&quot;

答案4

得分: 0

这是我试图避免的糟糕循环方法:

my_str <- ""
for (row in 1:nrow(df)) {
  my_str <- paste0(my_str, 
                   df[row, "state"], " ", df[row, "freq"])
  if (row < nrow(df)) {
    my_str <- paste0(my_str, ", ")
  }
}

输出结果:

> my_str
[1] "buy 0.102, sell 0.801"
英文:

This is my cheesy loop method which I am trying to avoid:

my_str &lt;- &quot;&quot;
for (row in 1:nrow(df)) {
  my_str &lt;- paste0(my_str, 
                   df[row, &quot;state&quot;], &quot; &quot;, df[row, &quot;freq&quot;])
  if (row &lt; nrow(df)) {
    my_str &lt;- paste0(my_str, &quot;, &quot;)
  }
}

&gt; my_str
[1] &quot;buy 0.102, sell 0.801&quot;

huangapple
  • 本文由 发表于 2023年5月24日 23:40:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76325270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定