MikeJewski
MikeJewski

Reputation: 357

R Sorting Matrix name, including 1 string but excluding the other

I am currently trying to sort a matrix, and for the life of me can't figure out how to do this. I have rownames that consist of 2 words, and I want to be able to compare them by sorting these 2 words. Currently I am able to use grep to sort for the 2 words that I want. Here is a quick example.

x <- matrix(NA,nrow=12,ncol=2)
multi <- 4
colour <- c("Red","Blue","Dark Blue")
colour <- as.vector(sapply(colour,function(y) rep(y,multi)))
food <- c("Water","Milk","Juice")
food <- as.vector(t(sapply(food,function(y) rep(y,multi))))
rownames(x) <- paste(food,colour,sep=" ,")

Milk <- x[grep(paste0("^Milk ,","Blue$"),rownames(x)),]

What I am trying to do now from the above example, is find a way to sort for everything except "Milk", but still have the colour "Blue". Any ideas?

This is the current output

            [,1] [,2]
Milk ,Blue   NA   NA
Milk ,Blue   NA   NA

What I would like is this. It would need to be able to differentiate between "Blue" and "Dark Blue".

       [,1] [,2]
Water ,Blue   NA   NA
Juice ,Blue   NA   NA

Upvotes: 1

Views: 61

Answers (2)

Roman
Roman

Reputation: 17648

You can use the logical version and run it twice, or you can split your data (first you have to transform it into a data.frame).,

# Create the data, including rownames with Dark Blue

x <- matrix(NA,nrow=12,ncol=2)
colour <- rep(c("Red" ,"Blue", "Green", "Dark Blue"), 3)
food <- rep(c("Water ","Milk", "Juice"), 4)
rownames(x) <- paste(food, colour, sep=" ,")

# only Blue
x[ !grepl("Milk|Dark Blue", rownames(x)) & grepl("Blue", rownames(x)), ]
# only Dark Blue
x[ !grepl("Milk", rownames(x)) & grepl("Dark Blue", rownames(x)), ]

# or use split to create a list (Now with the solution from Pierre Lafortune)
x1 <- x[grep("(?<!Milk ,)Blue", rownames(x), perl = TRUE),]
gr <- ifelse(grepl("Dark Blue", rownames(x1)), 1, 2) # factor 1== Dark Blue; 2==Blue
split(data.frame(x1), gr)

Upvotes: 1

Pierre L
Pierre L

Reputation: 28441

Try a negative lookbehind to specify instances of Blue that do not proceed Milk:

x[grep("(?<!Milk ),Blue", row.names(x), perl = TRUE),]
            [,1] [,2]
Juice ,Blue   NA   NA
Water ,Blue   NA   NA

Upvotes: 3

Related Questions