简体   繁体   English

R:子集向量的名称

[英]R: Subset vector by names

I have a vector with different names and its values. 我有一个具有不同名称及其值的向量。 It is called composite: 它称为复合:

GSM12    GSM13   GSM15   GSM16  GSM17
0.1234   9.345   8.888   5.345  1.234

And I have a second vector with names which are important.I only want those names with its values. 我还有第二个向量,其名称很重要。我只希望这些名称带有其值。 The other names could be deleted. 其他名称可以删除。 The vector is called biopsies. 该载体称为活检。

GSM12  GSM15   GSM16

The result should be like this: 结果应该是这样的:

GSM12    GSM15   GSM16
0.1234   8.888   5.345

I tried the subset() function but it didn't work. 我尝试了subset()函数,但是没有用。 I also tried this: 我也试过这个:

composite[apply(sapply(biopsies, grepl, composite), 1, any)]

But it also didn't work. 但这也没有用。 So how can I do it? 那我该怎么办呢? Thanks 谢谢

x <- c(0.1234,   9.345,   8.888,  5.345,  1.234)
names(x) <- c("GSM12",  "GSM13",   "GSM15",   "GSM16",  "GSM17")
y <- c("GSM12", "GSM15",  "GSM16")

as @Gregor mentioned: 正如@Gregor提到的:

x[y]

 GSM12  GSM15  GSM16 
0.1234 8.8880 5.3450 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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