删除重复行的特定列值中的值

时间:2015-11-01 06:49:24

标签: r

我有以下数据框(df1):

First Name  Second Name Phone 
A                B       1     
A                B       2     
C                D       3     
C                D       4   

我想隐藏列First Name和Second Name值,只有它们重复,但相关的不同Phone列将保留在后续行中。

结果数据帧(df2)如下:

First Name  Second Name Phone 
A                B       1    
                         2    
C                D       3     
                         4    

1 个答案:

答案 0 :(得分:2)

我们可以使用duplicated

df2 <- df1
df2[1:2] <- lapply(df2[1:2], function(x) replace(x, duplicated(x), ''))
df2
#  FirstName SecondName Phone
#1         A          B     1
#2                          2
#3         C          D     3
#4                          4

如果“重复”仅表示相邻值

df2[1:2] <- lapply(df2[1:2] , function(x) replace(x ,c(FALSE, 
                              x[-1]==x[-length(x)]), ''))

在这种情况下,

v1 <- c('A', 'C', 'A', 'C')
v2 <- c('A', 'D', 'C', 'A')

返回完整的向量

replace(v1, c(FALSE, v1[-1]==v1[-length(v1)]), '')
#[1] "A" "C" "A" "C"

数据

df1 <- structure(list(FirstName = c("A", "A", "C", "C"), 
SecondName = c("B", 
"B", "D", "D"), Phone = 1:4), .Names = c("FirstName", "SecondName", 
"Phone"), class = "data.frame", row.names = c(NA, -4L))