减去特定行并重命名它
是否可以减去某些行并重命名它们?
year <- c(2005,2005,2005,2006,2006,2006,2007,2007,2007)
category <- c("a","b","c","a","b","c", "a", "b", "c")
value <- c(2,2,10,3,3,12,4,4,16)
df <- data.frame(year, category,value, stringsAsFactors = FALSE)
结果应如下所示:
年 | 类别 | 值 |
---|---|---|
2005 | a | 2 |
2005 | b | 2 |
2005 | c | 4 |
2006 | a | 3 |
2006 | b | 3 |
2006 | c | 12 |
2007 | a | 4 |
2007 | b | 4 |
2007 | c | 16 |
2005 | CB | 2 |
2006 | CB | 9 |
2007 | CB | 12 |
it is possible to subtract certain rows and rename them?
year <- c(2005,2005,2005,2006,2006,2006,2007,2007,2007)
category <- c("a","b","c","a","b","c", "a", "b", "c")
value <- c(2,2,10,3,3,12,4,4,16)
df <- data.frame(year, category,value, stringsAsFactors = FALSE)
And this is how the result should look:
year | category | value |
---|---|---|
2005 | a | 2 |
2005 | b | 2 |
2005 | c | 4 |
2006 | a | 3 |
2006 | b | 3 |
2006 | c | 12 |
2007 | a | 4 |
2007 | b | 4 |
2007 | c | 16 |
2005 | c-b | 2 |
2006 | c-b | 9 |
2007 | c-b | 12 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
group_modify
:You can use
group_modify
:请参阅
substract()
函数。示例:
如果您想知道正在处理哪些行,请使用
which()
您可以将每个所需行重命名为
See
substract()
function.Example:
If you want to know which rows are you dealing with, use
which()
You can rename each desired row as