我连接到postgresql tbl并将其命名为def0113。当我需要做一个 使用dcast的宽格式表我发现唯一的方法是代码中的显式data.frame。
#Connection to postgresql tbl. This table has three columns (variables)
#provre: state, grupedad: age group and sexo: sex
#and every row is a dead case.
db=src_postgres(dbname = 'mdb', user = "postgres",
password ="mypass", options="-c search_path=mortalidad")
def0113 = tbl(db, 'def0113')
def0113 %>%
filter(grupedad%like%'M%', provre == '30') %>%
data.frame() %>%
dcast(provre+sexo~grupedad)
结果是:
Using sitlabor as value column: use value.var to override.
Aggregation function missing: defaulting to length
provre sexo M1 M2 M3
1 30 1 89 39 47
2 30 2 31 20 47
3 30 3 5 1 2
4 30 9 6 0 0
有一种方法可以在没有%>%data.frame()?
的情况下执行此操作我也尝试过:
def0113 %>%
filter(grupedad%like%'M%', provre == '30') %>%
group_by(grupedad, provre, sexo) %>%
summarise(casos = n()) %>%
spread(grupedad, casos)
但我得到了这个
Error in UseMethod("spread_") :
no applicable method for 'spread_' applied to an object of class
"c('tbl_postgres', 'tbl_sql', 'tbl')"
然后我尝试了:
def0113 %>%
filter(grupedad%like%'M%', provre == '30') %>%
group_by(grupedad, provre, sexo) %>%
summarise(casos = n()) %>%
data.frame() %>%
spread(grupedad, casos)
我得到了预期的结果:
provre sexo M1 M2 M3
1 30 1 89 39 47
2 30 2 31 20 47
3 30 3 5 1 2
4 30 9 6 NA NA
为什么我需要创建data.frame。 def0113不是data.frame?。提前谢谢。