- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个函数可以找到两个散文串的交集:
# Function to get intersection of words
str_intersect_by_word_list <- function(string1, string2){
map2_chr(str_split(string1, '\\s'), str_split(string2, '\\s'),
~str_c(intersect(.x, .y), collapse = " "))
}
和一个包含要匹配的字符串的表格:
# Sample data
my_df <- tibble(
grp = rep(LETTERS[1:3], each = 3),
strng = c(
"Hi I'm Abe",
"Hi I'm Beau",
"Hi I'm Cat",
"Hi there I'm Doug",
"Hi there I'm Emily",
"Hi there I'm Finn",
"Hi it's nice to be here",
"Hi it's nice to meet you",
"Hi it's nice outside"
)
)
如果我想用公共(public)字符串创建一个列,我可以这样做:
# This works as expected
my_df %>%
mutate(
common_string = my_df %>%
pull(strng) %>%
reduce(str_intersect_by_word_list)
)
给出
# A tibble: 9 x 3
grp strng common_string
<chr> <chr> <chr>
1 A Hi I'm Abe Hi
2 A Hi I'm Beau Hi
3 A Hi I'm Cat Hi
4 B Hi there I'm Doug Hi
5 B Hi there I'm Emily Hi
6 B Hi there I'm Finn Hi
7 C Hi it's nice to be here Hi
8 C Hi it's nice to meet you Hi
9 C Hi it's nice outside Hi
我想用每个组共有的字符串创建一个列。但是,在分组内部,我只能访问 strng
的整个列,它提供与上面相同的输出,或者访问 strng
的当前值,这会导致错误,因为我的函数 str_intersect_by_word_list
需要两个输入。
我尝试引用 cur_data_all
,但我认为这不是该函数的预期用途,而且它给我的结果与上述相同。
# This fails.
my_df %>%
group_by(grp) %>%
mutate(
grp_string = cur_data_all() %>%
pull(strng) %>%
reduce(str_intersect_by_word_list)
)
预期的输出是
# A tibble: 9 x 3
grp strng grp_string
<chr> <chr> <chr>
1 A Hi I'm Abe Hi I'm
2 A Hi I'm Beau Hi I'm
3 A Hi I'm Cat Hi I'm
4 B Hi there I'm Doug Hi there I'm
5 B Hi there I'm Emily Hi there I'm
6 B Hi there I'm Finn Hi there I'm
7 C Hi it's nice to be here Hi it's nice
8 C Hi it's nice to meet you Hi it's nice
9 C Hi it's nice outside Hi it's nice
如何获取每个组的常用词?
最佳答案
请考虑做:
my_df %>%
group_by(grp) %>%
mutate(common_string = str_c(reduce(str_split(strng, "\\s+"), intersect),collapse = ' '))
# A tibble: 9 x 3
# Groups: grp [3]
grp strng common_string
<chr> <chr> <chr>
1 A Hi I'm Abe Hi I'm
2 A Hi I'm Beau Hi I'm
3 A Hi I'm Cat Hi I'm
4 B Hi there I'm Doug Hi there I'm
5 B Hi there I'm Emily Hi there I'm
6 B Hi there I'm Finn Hi there I'm
7 C Hi it's nice to be here Hi it's nice
8 C Hi it's nice to meet you Hi it's nice
9 C Hi it's nice outside Hi it's nice
关于在 mutate 中减少分组列上的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68353009/
我在 Nuxt 项目旁边使用 Firebase,在下面的插件中,我调用 onAuthStateChanged 来检查用户是否已经登录,如果他是,我设置用户状态并将他重定向到仪表板如下: import
这两个代码块都可以工作,即使它们使用不同的等号,一个使用 :=,另一个使用 =。哪个是正确的,为什么?我认为 tidyeval 在使用 dplyr 函数时需要 := ,但奇怪的是 = 在我的 muta
下午好! 我做了一些快速搜索,我很难弄清楚我应该如何去做我需要做的事情。 对于这个程序,我们正在创建一个基本的工作票类。每个属性都有自己的修改器和访问器,但除此之外还有一个修改器将所有属性作为参数并一
所以我有一个名为 VIP 的模型,其中包含大量相关信息。因此,当我们转到路线 vip/{id} 时,我会返回大部分信息。但是,当我转到 vips/{per-page} 时,我不想返回所有数据,因为 A
我有一个电子应用程序,它使用 mysql 包直接连接到我的数据库。我想做的是将使用 mysql.createConnection() 创建的 connection 对象存储在 Vuex 状态中。然后我
假设我有一个 Image 类,我想提供一些图像操作,比如缩放、旋转等。我想为每个操作提供两种类型的功能。一种修改对象,另一种不修改。在 Ruby 中,有些函数以 !并指出这个将修改参数。 因为这在 C
以下代码利用 DOM 突变事件 DOMNodeInserted检测 body 的存在元素并包裹它的 innerHTML放入 wrapper 中。 functi
我正在尝试从 Firestore 初始化我的 Vuex 商店。最后一行代码 context.commit('SET_ACTIVITIES', acts) 是产生错误的原因。我不认为我在直接改变状态,因
所以基本上我已经阅读了相当多的教程、演示和 API 规范本身,但并没有深入了解,非常感谢你们的帮助。 我最近一直在努力更好地掌握 IndexedDB,但遇到了一些问题,希望对这段代码提出一些批评/反馈
我有这个简单的示例代码: var request = mozIndexedDB.open('MyTestDatabase'); request.onsuccess = function(event){
我定义了一个 Vuex 存储( Action 、状态、突变和 getter) 当我在突变中向状态数组添加新的待办事项时,出现以下错误:错误:[vuex] 不要在突变处理程序之外改变 vuex 存储状态
事前:我的应用程序按预期工作,但我想知道是否有更好的方法来解决我遇到的问题。 情况:我有一个项目,目前正在实现权限系统。当前的流程是加载特定对象(在本例中让我们采用 user),然后注入(inject
这段代码 extension Collection { mutating func f() { removeFirst() } } 处理错误 cannot use mutating m
我们在 R 中有以下数据框 # Create example dataframe df % dplyr::mutate(col1A = ifelse(gp == 0, col1B, col1A))
在我的 NUXT 应用程序中,我正在使用 vuex 存储模块!当我运行应用程序并调用时 this.$store.dispatch('userStore/setLoggedInUser',current
所以我有这个数据集 # A tibble: 268 x 1 `Which of these social media platforms do you have an account in ri
转载请注明出处: 在 Vuex 中 store 数据改变的唯一方法就是提交 mutations 。 mutations 里面装着一些改变数据方法的集合,这是Vuex 设
我想用不同的变量多次调用一个函数,每次都为数据框中的一个新变量设置一个值。这是我失败的尝试。感谢您的帮助! dat % mutate({{var3}} := ifelse({{var1}} >
改变列表的正确方法是什么?在这种特定情况下,列表由 split 返回。 library(dplyr) csv%split(.,.$participant_number)%>%mutate(.,var(
在某些语言中比其他语言更难(或不可能)实现变异测试吗?例如,是否可以在功能编程语言中实现变异测试? 最佳答案 我看不出任何语言都无法做到的任何理由。 我当然不是专家,但是我认为使用功能语言进行突变测试
我是一名优秀的程序员,十分优秀!