5

入力

row.no   column2    column3  column4
1        bb         ee       up
2        bb         ee       down
3        bb         ee       up
4        bb         yy       down
5        bb         zz       up

行1と2と3を削除するルールがあります。これは、行1、2、3のcolumn2とcolumn3は同じですが、矛盾するデータ(up down)が列4にあるためです。

Rに、column2とcolumn3で同じ名前の行を削除するように依頼できますが、列3を縮小して、次のような行列を作成できます。

row.no   column2    column3  column4
4        bb         yy       down
5        bb         zz       up
4

4 に答える 4

6

パッケージ内の関数はplyr、このタイプの問題で本当に輝いています。これは、2行のコードを使用したソリューションです。

データを設定します(@GavinSimpsonから提供されました)

dat <- structure(list(row.no = 1:5, column2 = structure(c(1L, 1L, 1L, 
1L, 1L), .Label = "bb", class = "factor"), column3 = structure(c(1L, 
1L, 1L, 2L, 3L), .Label = c("ee", "yy", "zz"), class = "factor"), 
    column4 = structure(c(2L, 1L, 2L, 1L, 2L), .Label = c("down", 
    "up"), class = "factor")), .Names = c("row.no", "column2", 
"column3", "column4"), class = "data.frame", row.names = c(NA, 
-5L))

plyrパッケージをロードします

library(plyr)

ddplyデータを分割、分析、結合するために使用します。次のコード分析行は、datを(column2とcolumn3)の一意の組み合わせに個別に分割します。次に、uniqueという列を追加します。これは、各セットのcolumn4の一意の値の数を計算します。最後に、単純なサブセットを使用して、unique == 1の行のみを返し、列5を削除します。

df <- ddply(dat, .(column2, column3), transform, 
    row.no=row.no, unique=length(unique(column4)))
df[df$unique==1, -5]

そして結果:

  row.no column2 column3 column4
4      4      bb      yy    down
5      5      bb      zz      up
于 2011-04-17T08:30:27.837 に答える
4

これが、ややエレガントではないにしても、1つの潜在的な解決策です。

out <- with(dat, split(dat, interaction(column2, column3)))
out <- lapply(out, function(x) if(NROW(x) > 1) {NULL} else {data.frame(x)})
out <- out[!sapply(out, is.null)]
do.call(rbind, out)

これは次のようになります。

> do.call(rbind, out)
      row.no column2 column3 column4
bb.yy      4      bb      yy    down
bb.zz      5      bb      zz      up

いくつかの説明、行ごと:

  • 1行目:データをリストに分割します。各コンポーネントは、との一意の組み合わせによって形成されたグループに対応する行を持つデータフレームcolumn2ですcolumn3
  • 2行目: 1行目の結果を繰り返し処理します。データフレームに複数の行がある場合はNULLを返し、そうでない場合は1行のデータフレームを返します。
  • 3行目: 2行目からの出力を繰り返します。NULL以外のコンポーネントのみを返します
  • 4行目: 3行目からの出力を行ごとにバインドする必要があります。do.call()

これは、1〜3行目を1行にまとめて、2行に簡略化できます。

out <- lapply(with(dat, split(dat, interaction(column2, column3))),
              function(x) if(NROW(x) > 1) {NULL} else {data.frame(x)})
do.call(rbind, out[!sapply(out, is.null)])

上記はすべて次のように行われました。

dat <- structure(list(row.no = 1:5, column2 = structure(c(1L, 1L, 1L, 
1L, 1L), .Label = "bb", class = "factor"), column3 = structure(c(1L, 
1L, 1L, 2L, 3L), .Label = c("ee", "yy", "zz"), class = "factor"), 
    column4 = structure(c(2L, 1L, 2L, 1L, 2L), .Label = c("down", 
    "up"), class = "factor")), .Names = c("row.no", "column2", 
"column3", "column4"), class = "data.frame", row.names = c(NA, 
-5L))
于 2011-04-17T06:42:25.950 に答える
4

ギャビンは答えの質の水準を上げ続けています。これが私の試みです。

# This is one way of importing the data into R
sally <- textConnection("row.no   column2    column3  column4
1        bb         ee       up
2        bb         ee       down
3        bb         ee       up
4        bb         yy       down
5        bb         zz       up")
sally <- read.table(sally, header = TRUE)

# Order the data frame to make rle work its magic
sally <- sally[order(sally$column3, sally$column4), ]

# Find which values are repeating
sally.rle2 <- rle(as.character(sally$column2))
sally.rle3 <- rle(as.character(sally$column3))
sally.rle4 <- rle(as.character(sally$oclumn4))

sally.can.wait2 <- sally.rle2$values[which(sally.rle3$lengths != 1)]
sally.can.wait3 <- sally.rle3$values[which(sally.rle3$lengths != 1)]
sally.can.wait4 <- sally.rle4$values[which(sally.rle4$lengths != 1)]

# Find which lines have values that are repeating
dup <- c(which(sally$column2 == sally.can.wait2),
         which(sally$column3 == sally.can.wait3),
         which(sally$column4 == sally.can.wait4))
dup <- dup[duplicated(dup)]

# Display the lines that have no repeating values
sally[-dup, ]
于 2011-04-17T07:40:28.520 に答える
-1

次の2つの方法のいずれかを試すことができます。テーブルの名前が「table1」であるとします。

方法1

repeated_rows = c();
for (i in 1:(nrow(table1)-1)){
  for (j in (i+1):nrow(table1)){
    if (sum((table1[i,2:3] == table1[j,2:3])) == 2){
      repeated_rows = c(repeated_rows, i, j)
    }
  }
}
repeated_rows = unique(repeated_rows)
table1[-repeated_rows,]

方法2

duplicates = duplicated(table1[,2:3])
for (i in 1:length(duplicates)){
  if (duplicates[i] == TRUE){
    for (j in 1:nrow(table1)){
      if (sum(table1[i,2:3] == table1[j,2:3]) == 2){
        duplicates[j] = TRUE;
      }
    }
  }
}
table1[!duplicates,]
于 2011-04-17T05:43:20.103 に答える