我花了大约 20 分钟浏览以前的问题,但找不到我要找的东西。我有一个大数据框,我想根据名称列表对其进行子集化,但是数据框中的名称也可以有列表中未指明的后缀。
换句话说,是否有一种更简单的通用方法(对于无限数量的后缀)来执行以下操作:
data <- data.frame("name"=c("name1","name1_post1","name2","name2_post1",
"name2_post2","name3","name4"),
"data"=rnorm(7,0,1),
stringsAsFactors=FALSE)
names <- c("name2","name3")
subset <- data[ data$name %in% names | data$name %in% paste0(names,"_post1") | data$name %in% paste0(names,"_post2") , ]
回应@Arun的回答。我的数据中的名称实际上包含多个下划线,使问题更加复杂。
data <- data.frame("name"=c("name1_target_time","name1_target_time_post1","name2_target_time","name2_target_time_post1",
"name2_target_time_post2","name3_target_time","name4_target_time"),
"data"=rnorm(7,0,1),
stringsAsFactors=FALSE)
names <- c("name2_target_time","name3_target_time")
subset <- data[ data$name %in% names | data$name %in% paste0(names,"_post1") | data$name %in% paste0(names,"_post2") , ]