R 将列与日期相加
我的数据如下所示:R 将列与日期相加,r,dplyr,R,Dplyr,我的数据如下所示: structure(list(did = c(216L, 221L, 221L, 221L, 221L, 221L, 221L, 221L, 214L, 214L, 214L, 221L, 221L, 221L, 221L, 221L, 221L, 214L, 221L, 221L, 221L, 221L, 205L, 221L, 221L), fruid = c(1007L, 1697L, 1697L, 1698L, 1697L, 1697L, 1697L, 1697
structure(list(did = c(216L, 221L, 221L, 221L, 221L, 221L, 221L,
221L, 214L, 214L, 214L, 221L, 221L, 221L, 221L, 221L, 221L, 214L,
221L, 221L, 221L, 221L, 205L, 221L, 221L), fruid = c(1007L, 1697L,
1697L, 1698L, 1697L, 1697L, 1697L, 1697L, 2074L, 2074L, 2074L,
2087L, 2087L, 2087L, 2087L, 2087L, 2087L, 2074L, 2087L, 2087L,
3884L, 3884L, 2249L, 2087L, 1461L), hhid = c(89L, 5258L, 5256L,
5192L, 5301L, 5289L, 5296L, 5255L, 16413L, 8911L, 21550L, 5683L,
12001L, 5761L, 5667L, 5682L, 11988L, 21553L, 11996L, 11986L,
12158L, 12191L, 10367L, 5676L, 1130L), isenterprise = c(1L, 1L,
0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 1L, 1L, 1L, 0L, 1L), sale = c(11, 1750, 0, 4000, 0, 0,
2450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1600, 0, 0
), purchase = c(255, 750, 0, 3838, 0, 0, 1700, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), cash = c(99, 520, 0,
1150, 0, 0, 1585, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2500, 0, 0), asset = c(11, 1750, 0, 4000, 0, 0, 2450, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1600, 0, 0), islivestock = c(0L,
0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), lstype = c("0", "0", "Goatry",
"0", "Goatry", "Goatry", "0", "Goatry", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0"),
isagriculture = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), tsavings = c(5L, 1120L, 1200L, 2150L, 2700L, 4220L, 400L,
400L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 840L, 0L, 0L), month_year = c("Dec - 2019", "Dec - 2019",
"Dec - 2019", "Dec - 2019", "Dec - 2019", "Dec - 2019", "Dec - 2019",
"Dec - 2019", "Jan - 2020", "Jan - 2020", "Jan - 2020", "Jan - 2020",
"Jan - 2020", "Jan - 2020", "Jan - 2020", "Jan - 2020", "Jan - 2020",
"Jan - 2020", "Jan - 2020", "Jan - 2020", "Jan - 2020", "Jan - 2020",
"Jan - 2020", "Jan - 2020", "Jan - 2020")), row.names = c(NA,
25L), class = "data.frame")
预期的输出如下:
did fruid month_year hhid times_visited isenterprise_count t_sale t_purchase t_cash t_asset t_saving islivestock_count t_sale t_purchase t_cash t_asset t_saving isagriculture_count t_sale t_purchase t_cash t_asset t_saving.
为了解释输出,我想计算hhid月\年的发生次数,同时计算销售-购买-现金、资产和储蓄的总和。您可以根据唯一值和所选列进行分组
library(dplyr)
df %>%
group_by(did, fruid, month_year) %>%
summarise(count = n(),
across(c(sale:islivestock, isagriculture:tsavings),
sum, na.rm = TRUE))
谢谢Ronak。我想为每个ismicroenterprise、islivestock和is agriculture捕获买卖现金资产和储蓄。我已经能够通过过滤所有三个数据帧并创建三个数据帧来实现这一点,但需要在一个数据帧中完成。按fruid month\u year hhid分组。然后计算hhid(hhid\u count)每年每个月。请告诉我是否有问题,然后您应该将
ismicroenterprise
和其他变量包含在groupby
中。尝试:df%>%groupby(did,fruid,month,year,isenterprise,islivestock,isagriculture)%>%summary(count=n(),cross(c(sale:asset,tsavings),sum,na.rm=TRUE))
Thank.It's working.我还想计算标记了这三个的hhid(iEnterprise将hh_enterprise称为新列名,iLisveStock称为hh_牲畜称为新列名,iAsagriculture称为hh_农业称为新列名)。这将有助于确定参与不同活动的hhid的编号。我认为你应该将其作为一个新问题而不是扩展它。