本文介绍了r-dplyr mutate引用新列本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"a"的数据框.

I have a data frame like this named 'a'.

   ID        V1
   1         -1 
   1          0 
   1          1 
   1        1000 
   1          0 
   1          1
   2         -1 
   2          0 
   2         1000 


...

我缩短此数据框以简短显示.

I shorten this data frame to show briefly.

现在我想使用条件mutate函数创建一个新列,但是它应该引用由mutate函数创建的新列.

And now I want to create a new column using conditional mutate function, but it should refer new column created by mutate function.

a %>%
  group_by(ID) %>%
    mutate(V2, ifelse(row_number() == 1, 1, 
      ifelse(V1 < 1000, 1,
      ifelse(V1 >= 1000, lag(V2) + 1))

错误:然后找不到'V2'"消息.

"Error: Then 'V2' not found" message is produced.

这就是我想要的结果.

   ID        V1       V2
   1         -1       1
   1          0       1
   1          1       1
   1        1000      2
   1          0       2
   1          1       2
   2         -1       1
   2          0       1
   2         1000     2

我如何得到这个?感谢您的帮助.

How to I get this? Thanks for your help.

推荐答案

我们可以尝试

a %>%
     group_by(ID) %>% 
     mutate(V2 = cumsum(V1 >= 1000)+1L)
#     ID    V1    V2
#  <int> <int> <int>
#1     1    -1     1
#2     1     0     1
#3     1     1     1
#4     1  1000     2
#5     1     0     2
#6     1     1     2
#7     2    -1     1
#8     2     0     1
#9     2  1000     2

数据

a <- structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), 
V1 = c(-1L, 
0L, 1L, 1000L, 0L, 1L, -1L, 0L, 1000L)), .Names = c("ID", "V1"
), class = "data.frame", row.names = c(NA, -9L))

这篇关于r-dplyr mutate引用新列本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 07:42