根据由不同列分组的列的总和创建百分比列?
我正在争夺一个庞大的数据集,而我的R技能非常新。我真的在努力理解术语和过程,但发现这是一项斗争,因为R条件对我来说通常没有意义。很抱歉这是一个愚蠢的问题。
我在不同地面覆盖百分比的不同地点有植物物种的数据。我想创建一个新的列支撑罩,该柱子将每个物种覆盖物的比例占特定地点所有物种总覆盖率的百分比。这与按场地区域计算覆盖率的百分比略有不同,因为它无视裸露的地面而没有植被。这是一个仅使用一个站点的简单计算,但是我有一百多个站点,需要对按位置进行分组的物种进行计算。所需的列输出是支撑杆。
SPECIES SITE COVER PROP-COVER(%)
1 1 10 7.7
2 1 20 15.4
3 1 10 7.7
4 1 20 15.4
5 1 30 23.1
6 1 40 30.8
2 2 20 22.2
3 2 50
5 2 10
6 2 10
1 3 5
2 3 25
3 3 40
5 3 10
我已经查看了循环并重复一遍,但看不到论点应该去了哪里。我进行的每次尝试都返回无效。
以下是我尝试过的一些示例,我确定这些东西是完全宽广的,但是我无法弄清楚从哪里开始或知道是否可能。
a<- for (i in data1$COVER) {
sum(data1$COVER[data1$SITE=="i"],na.rm = TRUE)
}
a
NULL
当涉及“循环”等工作方式时,我有一个重大的脑部障碍,没有任何阅读似乎有所帮助,但是也许我想做的事情是不可能的吗? :(
非常感谢您的查看。
I am wrangling with a huge dataset and my R skills are very new. I am really trying to understand the terminology and processes but finding it a struggle as the R-documentation often makes no sense to me. So apologies if this is a dumb question.
I have data for plant species at different sites with different percentages of ground-cover. I want to create a new column PROP-COVER which gives the proportion of each species' cover as a percentage of the total cover of all species in a particular site. This is slightly different to calculating percentage cover by site area as it is disregards bare ground with no vegetation. This is an easy calculation with just one site, but I have over a hundred sites and need to perform the calculation on species ground-cover grouped by site. The desired column output is PROP-COVER.
SPECIES SITE COVER PROP-COVER(%)
1 1 10 7.7
2 1 20 15.4
3 1 10 7.7
4 1 20 15.4
5 1 30 23.1
6 1 40 30.8
2 2 20 22.2
3 2 50
5 2 10
6 2 10
1 3 5
2 3 25
3 3 40
5 3 10
I have looked at for loops and repeat but I can't see where the arguments should go. Every attempt I make returns a NULL.
Below is an example of something I tried which I am sure is totally wide of the mark, but I just can't work out where to begin with or know if it is even possible.
a<- for (i in data1$COVER) {
sum(data1$COVER[data1$SITE=="i"],na.rm = TRUE)
}
a
NULL
I have a major brain-blockage when it comes to how 'for' loops etc work, no amount of reading about it seems to help, but perhaps what I am trying to do isn't possible? :(
Many thanks for looking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在基本R中:
在整理中,您可以做:
也可以将代码写为
n = cover/sum(cover)
,甚至n = prop.table(cover)
In Base R:
In tidyverse you can do:
The code could also be written as
n = COVER/sum(COVER)
or evenn = prop.table(COVER)