[英]Add Conditional Calculated Column to R Dataframe
I'm needing to add a column to a dataframe that marks "X" if column 1 is greater than 0 and the preceding row's column 1 was less than 0. 如果第1列大于0并且上一行的第1列小于0,则需要向标记为“ X”的数据框添加一列。
So given: 因此给出:
c1 = c(0,1,-1,1,2,0,1)
testdf <- data.frame(c1)
I'd like to add column "new" to testdf as: 我想将列“ new”添加到testdf中:
| c1 | new |
+----+-----+
| 0 | |
| 1 | |
| -1 | |
| 1 | X |
| 2 | |
| 0 | |
| 1 | |
I believe it will need the usage shift()
, which I understand enough to create a subset with the command subsetdf <- subset(testdf,c1>0 & shift(c1,1)<0)
我相信它将需要使用shift()
,我对它的理解足以用命令subsetdf <- subset(testdf,c1>0 & shift(c1,1)<0)
创建一个子集。
We can try 我们可以试试
i1 <- with(testdf, c(FALSE,c1[-1] >0 & c1[-length(c1)] < 0 ))
testdf$new <- ifelse(i1, 'X', '')
testdf$new
#[1] "" "" "" "X" "" "" ""
Or using dplyr
或使用dplyr
library(dplyr)
testdf %>%
mutate(new=c("", "X")[(c1>0 & lag(c1)< 0)+1L])
In the mutate
call we can also use ifelse
as in the other post. 在mutate
调用中,我们也可以像在其他帖子中一样使用ifelse
。
If you did want use shift
from data.table
, you could go about it this way: 如果你确实想利用shift
,从data.table
,你可以去了解这样说:
library(data.table)
testdf$c1_lag <- shift(testdf$c1, n=1L)
testdf$new <- ifelse(testdf$c1 > 0 & testdf$c1_lag < 0, "X", "")
testdf
# c1 c1_lag new
# 1 0 NA
# 2 1 0
# 3 -1 1
# 4 1 -1 X
# 5 2 1
# 6 0 2
# 7 1 0
Easy to do such column mutation with dplyr package and lag operator as follows: 使用dplyr软件包和lag运算符可以很容易地进行此类列突变,如下所示:
library(dplyr)
testdf <- testdf %>% mutate(new = ifelse(c1 > 0 & lag(c1) < 0, 'X', ''))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.