- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想为数据集中由 core.vars 定义的以下变量重新编码以下值 < 4 = -1, 4 = 0, > 4 = 1,并仍将其余变量保留在数据框中。
temp.df <- as.tibble (mtcars)
other.vars <- c('hp', 'drat', 'wt')
core.vars <- c('mpg', 'cyl', 'disp')
temp.df <- rownames_to_column (temp.df, var ="cars_id")
temp.df <- temp.df %>% mutate_if (is.integer, as.numeric)
我尝试了多种方法来实现这一点。使用case_when
, mutate
, recode
但没有运气。 recode
需要一个向量,所以我的想法是使用 case_when
创建一个向量或mutate
对于每个感兴趣的变量,然后重新编码值。但他们失败了。
temp.df <- temp.df %>%
mutate_at(.vars %in% (core.vars)), '< 4' = "-1", '4' = "0", '> 4' = "1")
Error: unexpected ',' in "temp.df <- temp.df %>% mutate_at(.vars %in% (core.vars)),"
temp.df <- temp.df %>%
mutate_at(vars(one_of(core.vars)), '< 4' = "-1", '4' = "0", '> 4' = "1")
Error in inherits(x, "fun_list") : argument ".funs" is missing, with no default
temp.df <- temp.df %>%
mutate (temp.df, case_when (vars(one_of(core.vars)), recode ('< 4' = "-1", '4' = "0", '> 4' = "1")))
Error in mutate_impl(.data, dots) : Column
temp.df
is of unsupported class data.frame
temp.df <- temp.df %>%
case_when (vars(one_of(core.vars)), recode ('< 4' = "-1", '4' = "0", '> 4' = "1"))
Error in recode.character(
< 4
= "-1",4
= "0",> 4
= "1") : argument ".x" is missing, with no default
temp.df <- temp.df %>% rowwise() %>% mutate_at(vars (core.vars),
funs (case_when (
recode(., '< 4' = -1, '0' = 0, '>4' = 1)
))) %>%
ungroup()`
Error in mutate_impl(.data, dots) : Evaluation error: Case 1 (
recode(mpg,
< 4= -1,
0= 0,
>4= 1)
) must be a two-sided formula, not a double. In addition: Warning message: In recode.numeric(mpg,< 4
= -1,0
= 0,>4
= 1) : NAs introduced by coercion
论坛上之前的问题包括如何对单个变量执行此操作,但是正如前面提到的,我有 100 个变量和 300 个样本,因此不能逐行单独输入它们。
理想情况下,最好不要创建单独的数据框然后进行连接,或者像 mutate 那样创建多个单独的变量。
我确信有一个 for 循环和/或 ifelse 方法,但我正在尝试使用 tidyverse 来实现目标。任何建议都会有帮助。
最佳答案
temp.df %>%
mutate_at(vars(one_of(core.vars)),
function(x) case_when(
x < 4 ~ -1,
x == 4 ~ 0,
x > 4 ~ 1
))
输出
# A tibble: 32 x 12
cars_id mpg cyl disp hp drat wt qsec vs am gear carb
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Mazda RX4 1 1 1 110 3.9 2.62 16.5 0 1 4 4
2 Mazda RX4 Wag 1 1 1 110 3.9 2.88 17.0 0 1 4 4
3 Datsun 710 1 0 1 93 3.85 2.32 18.6 1 1 4 1
4 Hornet 4 Drive 1 1 1 110 3.08 3.22 19.4 1 0 3 1
5 Hornet Sportabout 1 1 1 175 3.15 3.44 17.0 0 0 3 2
6 Valiant 1 1 1 105 2.76 3.46 20.2 1 0 3 1
7 Duster 360 1 1 1 245 3.21 3.57 15.8 0 0 3 4
8 Merc 240D 1 0 1 62 3.69 3.19 20 1 0 4 2
9 Merc 230 1 0 1 95 3.92 3.15 22.9 1 0 4 2
10 Merc 280 1 1 1 123 3.92 3.44 18.3 1 0 4 4
关于R:使用 recode、mutate 和 case_when 重新编码变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51619942/
我在 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(
在某些语言中比其他语言更难(或不可能)实现变异测试吗?例如,是否可以在功能编程语言中实现变异测试? 最佳答案 我看不出任何语言都无法做到的任何理由。 我当然不是专家,但是我认为使用功能语言进行突变测试
我是一名优秀的程序员,十分优秀!