快速和缺点是我在使用相同因素的条件汇总计数和聚合函数时遇到问题。
假设我有这个数据帧:
library(dplyr)
df = tbl_df(data.frame(
company=c("Acme", "Meca", "Emca", "Acme", "Meca", "Emca"),
year=c("2011", "2010", "2009", "2011", "2010", "2013"),
product=c("Wrench", "Hammer", "Sonic Screwdriver", "Fairy Dust",
"Kindness", "Helping Hand"),
price=c("5.67", "7.12", "12.99", "10.99", NA, FALSE)))
创建此数据帧(实质上):
company year product price
1 Acme 2011 Wrench 5.67
2 Meca 2010 Hammer 7.12
3 Emca 2009 Sonic Screwdriver 12.99
4 Acme 2011 Fairy Dust 10.99
5 Meca 2010 Kindness NA
... ... ... ... ...
n Emca 2013 Helping Hand FALSE
我们想要df <- group_by(df, company, year, product)
,然后在一个集合(即数据帧)中获取以下信息:
最高价格
summarize(df, count = n()) #satisfies first item obviously
我在尝试获取其他人时遇到问题。我想我需要使用管道运营商?如果是这样,任何人都可以提供一些指导吗?
这是我尝试过的,而且是明显错误的,但我不确定下一步该去哪里:
summarize(df,
total.count = n(),
count = filter(df, is.na(price)),
avg.price = filter(df, !is.na(price), price != FALSE),
max.price = max(filter(df, !is.na(price), price != FALSE))
是的,我已经审核了文档,我确信答案都在那里,但它们可能对我的理解来说太高级了。提前谢谢!
答案 0 :(得分:50)
假设您的原始数据集与您创建的数据集类似(即NA
为character
。您可以在使用na.strings
读取数据时指定read.table
。但是,我想会自动检测到NA。
price
列为factor
,需要转换为numeric
类。当您使用as.numeric
时,所有非数字元素(即"NA"
,FALSE)都会被强制显示NA
}并显示警告。
library(dplyr)
df %>%
mutate(price=as.numeric(as.character(price))) %>%
group_by(company, year, product) %>%
summarise(total.count=n(),
count=sum(is.na(price)),
avg.price=mean(price,na.rm=TRUE),
max.price=max(price, na.rm=TRUE))
我使用的是显示的dataset
(...
行除外)。
df = tbl_df(data.frame(company=c("Acme", "Meca", "Emca", "Acme", "Meca","Emca"),
year=c("2011", "2010", "2009", "2011", "2010", "2013"), product=c("Wrench", "Hammer",
"Sonic Screwdriver", "Fairy Dust", "Kindness", "Helping Hand"), price=c("5.67",
"7.12", "12.99", "10.99", "NA",FALSE)))