dcast和总结成一个函数 - 参数丢失 [英] dcast and summary into a function - argument lost
问题描述
result_check< - data% >%
group_by(column,target)%>%
summaryize(Unique_Elements = n())%>%
dcast(column_code〜target,value.var =Unique_Elements )
例如,如果我们采用以下数据集:
column1 target
AA YES
BB NO
BC NO
AA是
代码将根据目标变量进行数据集合的整合,如下所示:
column1是否
AA 2 0
BB 0 1
BC 0 1
这是我如何构造函数:
aggregate_per_group< - 函数(列){
data%>%
group_by(列,目标)%>%
总汇(Unique_Elements = n())%>%
dcast 〜 target,value.var =Unique_Elements)}
但我收到 - 错误:未知变量组通过:列。我知道这是一个基本问题,但是有什么线索呢为什么我在group_by中失去理由?
我尝试使用以下的group_by_以及require(dplyr),但是它们似乎无关。
我们可以使用表 R
表(数据)
如果我们对一个函数感兴趣,那么使用 group_by _
分发
从 tidyr
aggregate_per_group< - function(column){
data%>%
group_by_(column,target)%>%
summarize(Unique_Elements = ))%>%
spread(target,Unique_Elements,fill = 0)
}
库(dplyr)
库(tidyr)
aggregate_per_group (column1)
#column1 NO YES
#*< chr> < DBL> < DBL>
#1 AA 0 2
#2 BB 1 0
#3 BC 1 0
如果我们需要 dcast
从 reshape2
library(reshape2)
aggregate_per_group< - function(column){
data%>%
group_by_(column,target )%>%
summary(Unique_Elements = n())%>%
dcast(data =。,paste(column,'〜target'),
value.var = Unique_Elements,fill = 0)
}
aggregate_per_group(column1)
#column1否是
#1 AA 0 2
#2 BB 1 0
#3 BC 1 0
I am trying to turn the following code, which works properly, into a function.
result_check <- data %>%
group_by(column, target) %>%
summarise(Unique_Elements = n()) %>%
dcast(column_code ~ target, value.var="Unique_Elements")
For example, if we take the following dataset:
column1 target
AA YES
BB NO
BC NO
AA YES
The code would do the aggregate the dataset as per the target variable, like this:
column1 YES NO
AA 2 0
BB 0 1
BC 0 1
This is how I construct the function:
aggregate_per_group <- function(column) {
data %>%
group_by(column, target) %>%
summarise(Unique_Elements = n()) %>%
dcast(column ~ target, value.var="Unique_Elements")}
But I get - Error: unknown variable to group by : column. I know its a basic question, but any clues why I am loosing the argument in the group_by?
I have tried using the following imlementation "group_by_", as well as "require("dplyr")", but they seem unrelated.
We can use table
from base R
table(data)
If we are interested in a function, then use the group_by_
along with spread
from tidyr
aggregate_per_group <- function(column) {
data %>%
group_by_(column, "target") %>%
summarise(Unique_Elements = n()) %>%
spread(target, Unique_Elements, fill = 0)
}
library(dplyr)
library(tidyr)
aggregate_per_group("column1")
# column1 NO YES
# * <chr> <dbl> <dbl>
#1 AA 0 2
#2 BB 1 0
#3 BC 1 0
If we need the dcast
from reshape2
library(reshape2)
aggregate_per_group <- function(column) {
data %>%
group_by_(column, "target") %>%
summarise(Unique_Elements = n()) %>%
dcast(data = ., paste(column, '~ target'),
value.var="Unique_Elements", fill = 0)
}
aggregate_per_group("column1")
# column1 NO YES
#1 AA 0 2
#2 BB 1 0
#3 BC 1 0
这篇关于dcast和总结成一个函数 - 参数丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!