r - tidyverse 中的函数
问题描述
我是 tidyverse 的新手。我想创建具有中间功能的 tidyverse。我有一个结构
temp1 = sapply(df, function(x) .....)
temp2 = sapply(temp1, function(x) .......... )
temp3 = sapply(df, function(x) ..........)
temp = data.frame(temp2/temp3)
我想得到这样的东西
sapply(df, function(x) .......) %>% sapply(df, function(x) ....... )
%>% ......
PS Ronak Shah 要求提供可重复的示例
df = data.frame(a = c(1,2,3), b = c(1,2,3))
temp1 = sapply(df, function(x) x*3)
temp2 = sapply(temp1, function(x) x+4 )
temp3 = sapply(df, function(x) x/4)
temp = data.frame(temp2/temp3)
解决方案
据我所知,管道操作员不记得链的第一个块,只记得前一个块,因此您必须使用中间步骤。
但是,您可以将代码的第一部分简化为管道:
temp1 = df %>% sapply(function(x) x*3) %>% sapply(function(x) x+4)
temp = temp1/sapply(df, function(x) x/4)
推荐阅读
- hibernate - 使用 Hibernate 将实体映射到表时出现 BeanCreationException
- javascript - 移除显示:在@media 大小之间更改时无
- openscenegraph - 多渲染目标 (MRT) 和 OSG
- javascript - Ajax 调用 - 数据未加载到我的数据库中
- html - 使用 HTML5 Canvas 裁剪图像位置
- c++ - Qt Creator 无法在 Ubuntu 上解析 gvContext
- rest - 非奇异资源下的资源是否仍然是 RESTful?
- git - Gradle 更新 git 子模块
- python - 如何创建时间属性?
- typescript - 泛型函数的Typescript ReturnType