简体   繁体   中英

Find rows where all columns are equal in R

I have a data.frame in R. The data.frame has three columns: R, G, B that represent the red, green and blue color code. I want to filter all rows with a "gray" color where R == G == B. Here is my code:

    red <- c("30", "68", "28", "60", "90", "40", "10", "C8", "88", "38", 
  "08", "00", "D8", "20", "F8", "18", "B0", "F8", "A8", "F8", "F8", 
  "F8", "F8", "F8", "F8", "F8", "F8", "F8", "F8", "F8", "F8", "F8", 
  "F8", "F8", "F8", "F8", "F8", "D8", "F8", "F8", "E8", "F8", "B8", 
  "F8", "F8", "C8", "E8", "B0", "98", "E0", "F8", "F8", "78", "90", 
  "68", "A8", "D0", "88", "F8", "48", "F8", "60", "30", "80", "58", 
  "C0", "F8", "08", "28", "50", "00", "20", "A0", "00", "18", "70", 
  "00", "00", "00", "40", "F0", "70", "00", "00", "00", "00", "00", 
  "00", "F8", "F8", "F0", "F8", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00")

  green <- c("30", "68", "28", "60", "90", "40", "10", "C8", "88", "38", 
  "08", "00", "D8", "20", "28", "18", "B0", "08", "A8", "58", "20", 
  "70", "88", "38", "18", "50", "B0", "68", "98", "80", "48", "D0", 
  "A8", "10", "C8", "40", "E8", "F8", "78", "A0", "F8", "C0", "F8", 
  "E0", "30", "F8", "E8", "F8", "F8", "F8", "90", "D8", "F8", "F8", 
  "F8", "F8", "F8", "F8", "00", "F8", "B8", "F8", "F8", "F8", "F8", 
  "F8", "60", "F8", "F8", "F8", "F0", "F8", "F8", "E8", "F8", "F8", 
  "D8", "A8", "C8", "F8", "F8", "70", "80", "B8", "98", "68", "F8", 
  "48", "F8", "F0", "F0", "F8", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00")

  blue <- c("30", "68", "28", "60", "90", "40", "10", "C8", "88", "38", 
  "08", "00", "D8", "20", "00", "18", "B0", "00", "A8", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "E8", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "08", "00", "00", "10", "00", "00", 
  "20", "50", "30", "00", "00", "70", "78", "40", "60", "90", "00", 
  "B0", "00", "00", "F0", "F8", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", 
  "00", "00", "00", "00")

  RGB <- data.frame("R"=red, "G"=green, "B"=blue)
  grays <- subset(RGB,(R==G & R==B & G==B))

But on the last command I get an error: Error in Ops.factor(R, B) : level sets of factors are different

I also tried the subsetting command:

grays2 <- RGB[(RGB$R==RGB$G & RGB$R==RGB$B & RGB$G==RGB$B),]

But it gives me the same error message.

Anything I'm doing wrong here?

If you are willing to give data.table a try, the code is shorter. Still need to set stringsAsFactors=F as opined in the comments.

RGB <- data.frame("R"=red, "G"=green, "B"=blue, stringsAsFactors=F)
library(data.table)
setDT(RGB)  ## set data frame to data table
grays_dt <- RGB[c(R==G & R==B & G==B), ]  ## do the subsetting
grays_dt
#     R  G  B
# 1: 30 30 30
# 2: 68 68 68
# 3: 28 28 28
# 4: 60 60 60
# 5: 90 90 90
# ---         
# 181: 00 00 00
# 182: 00 00 00
# 183: 00 00 00
# 184: 00 00 00
# 185: 00 00 00

The difference between @Mark's solution and this one is that the column class is Factor in the base solution but Character in data.table solution.

str(grays_dt)
# Classes ‘data.table’ and 'data.frame':  185 obs. of  3 variables:
# $ R: chr  "30" "68" "28" "60" ...
# $ G: chr  "30" "68" "28" "60" ...
# $ B: chr  "30" "68" "28" "60" ...
# - attr(*, ".internal.selfref")=<externalptr> 

str(grays)
# 'data.frame': 185 obs. of  3 variables:
# $ R: Factor w/ 21 levels "00","08","10",..: 7 11 6 10 14 9 3 17 13 8 ...
# $ G: Factor w/ 21 levels "00","08","10",..: 7 11 6 10 14 9 3 17 13 8 ...
# $ B: Factor w/ 21 levels "00","08","10",..: 7 11 6 10 14 9 3 17 13 8 ...

Try this:

grayIndexes <-which(red==green & green==blue & red==green)
grays <- data.frame("R"=red[grayIndexes], 
                    "G"=green[grayIndexes], 
                    "B"=blue[grayIndexes])

If you were interested in the ones with colour:

colourIndexes <-which(!(red==green & green==blue & red==green))
colours <- data.frame("R"=red[colourIndexes], 
                      "G"=green[colourIndexes],
                      "B"=blue[colourIndexes])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM