减去行并创建新的行名称
我想在此数据框中从佛罗里达中减去贝县,并创建一个名为“佛罗里达(-贝县)”的新行。
也许 group_modify 和 add_row (dplyr) 是一种可能性?
year <- c(2005,2006,2007,2005,2006,2007,2005,2006,2007,2005,2006,2007)
county <- c("Alachua County","Alachua County","Alachua County","Baker County","Baker County","Baker County","Bay County","Bay County","Bay County","Florida","Florida","Florida")
pop <- c(3,6,8,9,8,4,5,8,10,17,22,22)
gdp <- c(3,6,8,9,8,4,5,8,10,17,22,22)
area <- c(3,6,8,9,8,4,5,8,10,17,22,22)
density<-c(3,6,8,9,8,4,5,8,10,17,22,22)
df <- data.frame(year, county,pop,gdp,area,density, stringsAsFactors = FALSE)
年份 | 县 | 人口 | GDP | 面积 | 密度 |
---|---|---|---|---|---|
2005 | 阿拉楚阿县 | 3 | 3 | 3 | 3 |
2005 | 贝克县 | 9 | 9 | 9 | 9 |
2005 | 贝县 | 5 | 5 | 5 | 5 |
2005 | 佛罗里达州 | 17 | 17 | 17 | 17 |
2005 | 佛罗里达州(-贝县) | 12 | 12 | 12 | 12 |
2006 | 阿拉楚阿县 | 6 | 6 | 6 | 6 |
2006 | 贝克县 | 8 | 8 | 8 | 8 |
2006 | 贝县 | 8 | 8 | 8 | 8 |
2006 | 佛罗里达州 | 22 | 22 | 22 | 22 |
2006 | 佛罗里达州(-贝县) | 14 | 14 14 | 14 | |
2007 | 阿拉楚阿县 | 8 | 8 | 8 | 8 |
2007 | 贝克县 | 4 | 4 | 4 | 4 |
2007 | 贝县 | 10 | 10 | 10 | 10 |
2007 | 佛罗里达州 | 22 | 22 | 22 | 22 |
2007 | 佛罗里达州(-贝县) | 12 | 12 | 12 | 12 |
I would like to subtract Bay County from Florida in this data frame and create a new row with the name "Florida (-Bay County)".
Maybe group_modify and add_row (dplyr) would be a possibility?
year <- c(2005,2006,2007,2005,2006,2007,2005,2006,2007,2005,2006,2007)
county <- c("Alachua County","Alachua County","Alachua County","Baker County","Baker County","Baker County","Bay County","Bay County","Bay County","Florida","Florida","Florida")
pop <- c(3,6,8,9,8,4,5,8,10,17,22,22)
gdp <- c(3,6,8,9,8,4,5,8,10,17,22,22)
area <- c(3,6,8,9,8,4,5,8,10,17,22,22)
density<-c(3,6,8,9,8,4,5,8,10,17,22,22)
df <- data.frame(year, county,pop,gdp,area,density, stringsAsFactors = FALSE)
year | county | pop | gdp | area | density |
---|---|---|---|---|---|
2005 | Alachua County | 3 | 3 | 3 | 3 |
2005 | Baker County | 9 | 9 | 9 | 9 |
2005 | Bay County | 5 | 5 | 5 | 5 |
2005 | Florida | 17 | 17 | 17 | 17 |
2005 | Florida (-Bay County) | 12 | 12 | 12 | 12 |
2006 | Alachua County | 6 | 6 | 6 | 6 |
2006 | Baker County | 8 | 8 | 8 | 8 |
2006 | Bay County | 8 | 8 | 8 | 8 |
2006 | Florida | 22 | 22 | 22 | 22 |
2006 | Florida (-Bay County) | 14 | 14 | 14 | 14 |
2007 | Alachua County | 8 | 8 | 8 | 8 |
2007 | Baker County | 4 | 4 | 4 | 4 |
2007 | Bay County | 10 | 10 | 10 | 10 |
2007 | Florida | 22 | 22 | 22 | 22 |
2007 | Florida (-Bay County) | 12 | 12 | 12 | 12 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想尝试使用
group_modify
和add_row
尝试尝试,则可以考虑这样的东西。在这里,使用add_row
,使用MAP
sum 将组中的数据添加到组中,但不包括“佛罗里达州”或“ Bay County”。输出
If you wanted to try something with
group_modify
andadd_row
, you could consider something like this. Here, when usingadd_row
, usemap
tosum
up the data within the group, but not including "Florida" or "Bay County".Output
你可以做:
You could do: