- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 test 的小数据集,我想在其中对向量 cm 中定义的列执行一些变异操作。
安装和加载一些包
require(devtools)
devtools::install_github("hadley/dplyr")
require(dplyr)
首先创建测试数据框
test <- data.frame(col1 = c(1,1),
col2=as.character(c(2,2)),
col3=as.character(c(3,3)), stringsAsFactors=F)
然后我们创建 cm 向量
cm <- c("col2", "col3")
我现在可以选择以厘米为单位的列
test %>% select(one_of(cm))
但是当我想执行一个操作时,比如 as.numeric 我得到一个错误。
> test %>% mutate_each(funs(as.numeric), one_of(cm))
Error in one_of(vars, ...) : object 'cm' not found
虽然我可以手动粘贴矢量
test %>% mutate_each(funs(as.numeric), one_of("col2","col3")) %>% str()
'data.frame': 2 obs. of 3 variables:
$ col1: num 1 1
$ col2: num 2 2
$ col3: num 3 3
这是错误还是功能?我错过了什么吗?还有其他方法吗?
谢谢!马丁
最佳答案
您需要安装并加载包lazyeval
,然后您可以使用以下选项之一:
require(lazyeval)
require(dplyr)
test %>%
mutate_each_(funs(as.numeric), interp(~one_of(cm), var = as.name(cm))) %>%
str()
或者,更短的版本:
test %>% mutate_each_(funs(as.numeric), cm) %>% str()
在这种情况下两者都会做同样的事情。
关于r - mutate_each 中的 one_of(vector) 导致找不到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27061792/
我正在使用 devtools::check() 运行 R CMD 检查对于我正在编写的包,我在检查中遇到以下注意事项: * checking R code for possible problems
为什么是 dplyr::one_of()叫那个?所有其他 select_helpers名字对我来说很有意义,所以我想知道 one_of() 是否有一个方面我不明白。 我对 one_of() 的理解是它
我有两个主要数据框,其中有两组我想删除的列名称: df1 tibble(a = c(1,2), b = c(3,4), c = c(5,6)) %>% select(-one_of("c","d",
我有两个主要数据框,其中有两组我想删除的列名称: df1 tibble(a = c(1,2), b = c(3,4), c = c(5,6)) %>% select(-one_of("c","d",
我有一个名为 test 的小数据集,我想在其中对向量 cm 中定义的列执行一些变异操作。 安装和加载一些包 require(devtools) devtools::install_github("
我的数据如下所示: library(dplyr) set.seed(123) df % mutate_if(is.numeric, funs(new = . - ref)) 如果我只想对列的子集
我是一名优秀的程序员,十分优秀!