R排序矩阵名称,包括1个字符串但不包括其他字符串

时间:2016-02-04 15:21:55

标签: r sorting grep

我目前正在尝试对矩阵进行排序,而对于我的生活却无法弄清楚如何做到这一点。我有两个单词组成的rownames,我希望能够通过对这两个单词进行排序来比较它们。目前我可以使用grep来排序我想要的2个单词。这是一个简单的例子。

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)),]

我现在尝试从上面的例子中做的是找到一种方法来排序除“牛奶”之外的所有东西,但仍然有“蓝色”的颜色。有什么想法吗?

这是当前的输出

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

我想要的是这个。它需要能够区分“蓝色”和“深蓝色”。

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

2 个答案:

答案 0 :(得分:3)

尝试使用否定的lookbehind来指定{not} Blue的{​​{1}}实例:

Milk

答案 1 :(得分:1)

您可以使用逻辑版本并运行两次,或者您可以拆分数据(首先必须将其转换为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)