[英]Referencing previous value in cell (R)
I have the following data.frame: 我有以下data.frame:
head(data.c)
mark high_mark mark_cum
5 0 0
7 1 1
7 1 2
NA 0 2
7 1 3
7 1 4
As there are NAs I need to construct an additional column of normal sequence from 1:length(mark). 由于存在NA,因此我需要从1:length(mark)构造一列正常序列的附加列。 However, if it is NA vector cell has to take a previous value. 但是,如果是NA,则矢量单元必须取先前的值。 So it must look like this: 所以它必须看起来像这样:
mark high_mark mark_cum mark_seq
5 0 0 1
7 1 1 2
7 1 2 3
NA 0 2 3
7 1 3 4
7 1 4 5
NA 0 4 5
1) cumsum This solution uses the fact that each mark_seq
element equals the cumulative number of non-NA elements in mark
at that point. 1)累积此解决方案利用了以下事实:每个mark_seq
元素都等于该点上mark
中非NA元素的累积数量。
transform(data.c, mark_seq = cumsum(!is.na(mark)))
giving: 给予:
mark high_mark mark_cum mark_seq
1 5 0 0 1
2 7 1 1 2
3 7 1 2 3
4 NA 0 2 3
5 7 1 3 4
6 7 1 4 5
7 NA 0 4 5
data.c <- read.table(text = Lines, header = TRUE)
2) na.locf Here is a second solution using seq_along
and na.locf
(from zoo). 2)na.locf这是使用seq_along
和na.locf
(来自zoo)的第二个解决方案。 It creates a sequence the same length as the number of non-NA elements in mark
and uses replace to put them in the spots where the non-NA elements exist. 它创建一个长度与mark
中非NA元素数量相同的序列,并使用替换将其放置在非NA元素存在的位置。 Then na.locf
is used to fill in the NAs with the prior values. 然后使用na.locf
用先前的值填充NA。
library(zoo)
transform(data.c, mark_seq=na.locf(replace(mark, !is.na(mark), seq_along(na.omit(mark)))))
3) mark_cum It was not stated in the question how the input column mark_cum
is constructed but in the sample output in the question the mark_seq
column equals the mark_cum
column plus 1 so if that is always the case then an easy solution is: 3)mark_cum在问题中并未说明输入列mark_cum
的构造方式,但在问题的示例输出中, mark_seq
列等于mark_cum
列加1,因此,如果总是这样,那么一个简单的解决方案是:
transform(data.c, mark_seq = mark_cum + 1)
Note: We used this as the input: 注意:我们以此为输入:
Lines <- "mark high_mark mark_cum
5 0 0
7 1 1
7 1 2
NA 0 2
7 1 3
7 1 4
NA 0 4"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.