通过拆分数字来添加列,并删除重复项

时间:2018-08-13 12:20:12

标签: r dataframe unique

我有一个如下数据框(这是一个简化的示例,我有更多的行和列):

    CH1  CH2  CH3
1  3434  282 7622
2  4442 6968 8430
3  4128 6947  478
4  6718 6716 3017
5  3735 9171 1128
6    65 4876 4875
7  9305 6944 3309
8  4283 6060  650
9  5588 2285  203
10  205 2345 9225
11 8634 4840  780
12 6383    0 1257
13 4533 7692 3760
14 9363 9846 4697
15 3892   79 4372
16 6130 5312 9651
17 7880 7386 6239
18 8515 8021 2295
19 1356   74 8467
20 9024 8626 4136

我需要通过拆分值来创建其他列。例如,值1356必须分为656356。我是在for循环中按字符串分割的。我这样做是为了保持前导零。到目前为止,还不错。

# CREATE ADDITIONAL COLUMNS
for(col in 1:3) {

  # Create a temporal variable
  temp <- as.character(data[,col] )

  # Save the new column
  for(mod in c(-1, -2, -3)) {
    # Create the column
    temp <- cbind(temp, str_sub(as.character(data[,col]), mod))
  }

  # Merge to the row
  data <- cbind(data, temp)

}

我的问题是,并非所有单元格都有4位数字:有些可能有1位,2位或3位数字。因此,拆分时会得到重复的值。例如,对于79,我得到:79(原始),9797979

问题:我需要删除重复的值。当然,我可以做unique,但这使我的行数不均匀。我需要用NA填充那些丢失的内容(即删除的重复值)。我只能逐行比较。

我检查了CJ Yetman's answer here,但它们仅替换连续的数字。我只需要保留唯一值。

可复制的示例:这是我的代码起作用的小提琴:http://rextester.com/IKMP73407

预期结果:例如,对于示例的第11行和第12行(请参见可复制示例的链接),如果这是我的原始照片:

8634  4 34 634 4840  0 40 840  780  0 80 780    
6383  3 83 383    0  0  0   0 1257  7 57 257

我想得到这个:

8634  4 34 634 4840  0 40 840  780  NA 80 NA    
6383  3 83 383    0  NA  NA   NA 1257  7 57 257

1 个答案:

答案 0 :(得分:2)

您可以使用apply()

数据:

data <- structure(list(CH1 = c(3434L, 4442L, 4128L, 6718L, 3735L, 65L, 
                               9305L, 4283L, 5588L, 205L, 8634L, 6383L, 4533L, 9363L, 3892L, 
                               6130L, 7880L, 8515L, 1356L, 9024L), CH2 = c(282L, 6968L, 6947L, 
                                                                           6716L, 9171L, 4876L, 6944L, 6060L, 2285L, 2345L, 4840L, 0L, 7692L, 
                                                                           9846L, 79L, 5312L, 7386L, 8021L, 74L, 8626L), CH3 = c(7622L, 
                                                                                                                                 8430L, 478L, 3017L, 1128L, 4875L, 3309L, 650L, 203L, 9225L, 780L, 
                                                                                                                                 1257L, 3760L, 4697L, 4372L, 9651L, 6239L, 2295L, 8467L, 4136L
                                                                           )), .Names = c("CH1", "CH2", "CH3"), row.names = c(NA, 20L), class = "data.frame")

选择第11和12行:

data <- data[11:12, ]

使用您的代码:

# CREATE ADDITIONAL COLUMNS
for(col in 1:3) {

  # Create a temporal variable
  temp <- data[,col]

  # Save the new column
  for(mod in c(10, 100, 1000)) {
    # Create the column
    temp <- cbind(temp, data[, col] %% mod)
  }

  data <- cbind(data, temp)
}


data[,1:3] <- NULL

结果是:

   temp V2 V3  V4 temp V2 V3  V4 temp V2 V3  V4
11 8634  4 34 634 4840  0 40 840  780  0 80 780
12 6383  3 83 383    0  0  0   0 1257  7 57 257

然后逐行浏览数据,删除重复项并转置结果:

t(apply(data, 1, function(row) {
  row[duplicated(row)] <- NA
  return(row)
}))

结果是:

   temp V2 V3  V4 temp V2 V3  V4 temp V2 V3  V4
11 8634  4 34 634 4840  0 40 840  780 NA 80  NA
12 6383  3 83 383    0 NA NA  NA 1257  7 57 257