英文:
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 <- data.frame (state=c("buy", "sell"), freq=c(0.102, .801))
so it looks like this:
> df
state freq
1 buy 0.102
2 sell 0.801
I want to get the string:
mystring <- "buy 10.2%, sell 80.1%"
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, "{state} {freq*100}%") |>
str_flatten_comma()
# [1] "buy 10.2%, sell 80.1%"
答案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)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df <- data.frame (state=c("buy", "sell"), freq=c(0.102, .801))
df %>%
mutate(together = paste0(state, " ", freq * 100, "%")) %>%
pull(together) %>%
paste0(collapse = ", ")
#> [1] "buy 10.2%, sell 80.1%"
<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 %>%
mutate(freq = paste0(round(freq * 100, 1), "%")) %>%
pmap_vec(paste, sep = " ") %>%
paste(collapse = ", ")
[1] "buy 10.2%, sell 80.1%"
答案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 <- ""
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论