在特定行添加一个带有累积元素数量的列。

huangapple go评论77阅读模式
英文:

Add column with the cumulative number of elements found in a specific row

问题

这个表格基于一个物种抽样过程,包括从森林中的某个特定位置开始,记录在该确切位置出现的物种数量。然后,调查员步行并记录他走了多远才发现一个新物种。这是他发现新物种和初始点之间的距离。

我想创建一个新的列,其中包括基于行进距离的累积物种数量。新列应该如下所示。

英文:

This table is based on a species sampling procedure that comprises starting at a certain location in a forest and recording the number of species that occur in that exact spot. Then, the surveyor walks and records the distance he traveled until he found a new species. This is the distance between the place where he found the new species and the initial point.

在特定行添加一个带有累积元素数量的列。

I would like to create a new column the includes the cumulative number of species based on the traveled distance. Here's what the new column should look like.

在特定行添加一个带有累积元素数量的列。

Example data:

data<-structure(list(id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), binomial = c("Dromicodryas bernieri", 
"Dromicodryas quadrilineatus", "Erymnochelys madagascariensis", 
"Furcifer lateralis", "Furcifer oustaleti", "Hemidactylus mercatorius", 
"Langaha pseudoalluaudi", "Leioheterodon madagascariensis", "Lycodryas pseudogranuliceps", 
"Liophidium torquatum", "Liopholidophis sexlineatus", "Madagascarophis colubrinus", 
"Madatyphlops decorsei", "Madascincus polleni", "Mimophis mahfalensis", 
"Pelusios castanoides", "Phelsuma madagascariensis", "Thamnosophis lateralis", 
"Trachylepis elegans", "Trachylepis gravenhorstii", "Zonosaurus madagascariensis", 
"Hemidactylus frenatus", "Calumma nasutum", "Trachylepis madagascariensis", 
"Amphiglossus macrocercus", "Zonosaurus aeneus", "Phelsuma lineata", 
"Pelomedusa subrufa", "Calumma crypticum", "Furcifer viridis", 
"Lygodactylus blancae", "Calumma gastrotaenia", "Trachylepis boettgeri", 
"Zonosaurus ornatus", "Sanzinia madagascariensis", "Oplurus cyclurus", 
"Leioheterodon modestus", "Oplurus cuvieri", "Madascincus igneocaudatus", 
"Acrantophis dumerili", "Furcifer campani", "Pseudoxyrhopus imerinae", 
"Lygodactylus mirabilis", "Phelsuma barbouri", "Furcifer minor", 
"Compsophis infralineatus", "Pseudoxyrhopus quinquelineatus", 
"Calumma hilleniusi", "Paroedura bastardi", "Brookesia brygooi"
), distance = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 3714.77402549982, 6249.49093233716, 7067.80424387549, 
7715.0303317613, 13769.1057463018, 17206.1480236598, 18733.5237644898, 
21923.789153995, 27314.2085865309, 31154.1890492383, 35460.0864839256, 
35822.0263564291, 36933.3736660544, 39735.6007540156, 40983.6673876956, 
43032.8409122139, 43793.3004333338, 44063.3992480126, 44657.9183000201, 
44723.8214805486, 45184.0884859559, 46785.9008560645, 48994.7048866502, 
55332.621992021, 57746.4142325833, 58866.2845249788, 60839.811988087, 
65560.1987963227)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-50L))

答案1

得分: 2

一种选择是对逻辑向量进行cumsum,即距离不等于0的地方,然后将距离为0的计数添加到其中(sum(distance == 0)

library(dplyr)
data %>% 
  mutate(new = cumsum(distance != 0) + sum(distance == 0)) 

或者,我们可以使用base R来完成这个任务

data$new <- with(data, cumsum(distance != 0) + sum(distance == 0))
英文:

One option is to get the cumsum on a logical vector i.e. where distance not equal to 0 and then add the count of 0 distance to it (sum(distance == 0))

library(dplyr)
data &lt;- data %&gt;%
  mutate(new = cumsum(distance!=0) + sum(distance == 0)) 

Or for this, we can use base R

data$new &lt;- with(data, cumsum(distance!=0) + sum(distance == 0))

huangapple
  • 本文由 发表于 2023年1月9日 01:29:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定