R - 测试值是否与

时间:2017-06-20 17:52:08

标签: r if-statement dataframe dplyr na

我有以下df:

name   color
A      red 
B      red
C      green
D      red
E      red
F      red

我想测试'color'列中的值,看看它们是否与上面一行中的值相同,并写入一个新列...我可以使用以下代码:

> df$same <- ifelse(df$color == df$color[c(NA,1:(nrow(df)-1))], 1, 0)

给我:

name   color  same
A      red      NA
B      red       1
C      green     0
D      red       0
E      red       1
F      red       1

但是有更清洁的方法吗? (我一直用这个)......

3 个答案:

答案 0 :(得分:1)

您可以尝试lag包中的dplyr功能。您可以使用上面一行的值创建一个新列,然后比较它们,

> dt$color_above <- lag(dt$color, n=1)

> dt   

  name color color_above
1    A   red        <NA>
2    B   red         red
3    C green         red
4    D   red       green
5    E   red         red
6    F   red         red

或直接解决问题,您可以使用magrittr包中的管道运算符。它仍然冗长,但我认为它使代码更加清晰。

> dt %$%  
    { color == lag(color, n=1) } %>% 
    as.numeric() %>% 
    {.} -> dt$same

> dt

  name color same
1    A   red   NA
2    B   red    1
3    C green    0
4    D   red    0
5    E   red    1
6    F   red    1

答案 1 :(得分:0)

添加到Rafael的答案,您可以Found a swap file by the name "~/.bashrc.swp" owned by: Shawn dated: Wed Jun 21 16:01:37 2017 file name: ~Shawn/.bashrc modified: YES user name: Shawn host name: Shawns-MBP.domain process ID: 39328 While opening file "/Users/Shawn/.bashrc" dated: Wed Jun 21 16:19:11 2017 NEWER than swap file! (1) Another program may be editing the same file. If this is the case, be careful not to end up with two different instances of the same file when making changes. Quit, or continue with caution. (2) An edit session for this file crashed. If this is the case, use ":recover" or "vim -r /Users/Shawn/.bashrc" to recover the changes (see ":help recovery"). If you did this already, delete the swap file "/Users/Shawn/.bashrc.swp" to avoid this message. 使用ifelse

dplyr::mutate

答案 2 :(得分:0)

如何在标准R中做到这一点(可能更具可读性,但不比您短):

colour <- c("red","red","green","red","red","red")
(c(NA, colour) == c(colour, NA))[1:length(colour)]

[1]    NA  TRUE FALSE FALSE  TRUE  TRUE