【问题标题】:Adding columns by splitting number, and removing duplicates通过拆分数字添加列,并删除重复项
【发布时间】:2018-08-13 12:20:12
【问题描述】:

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

    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

【问题讨论】:

  • 请显示示例的预期输出
  • 我的函数的结果在链接上,代码工作的地方。但是,我在帖子中添加了更多信息。
  • 有些疑惑。例如。 8021 1 21 21 显示不同。另外,为什么 780 拆分成 NA 80 NA 而不是 0 80 780。你想如何拆分65205
  • 我改了功能。 8021 将拆分为 021、21、1,其中 021 与 21 不同。780 拆分为那个,因为行上已经出现了零,并且 780(最后一个拆分)与原始数字相同(!)。
  • 看起来你把函数从%%改成了str_sub

标签: r dataframe unique


【解决方案1】:

你可以使用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

【讨论】:

    猜你喜欢
    • 2019-01-02
    • 2017-09-11
    • 2017-11-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    相关资源
    最近更新 更多