英文:
Filter vector based on values from a column
问题
让我们假设我有一个向量
letters <- c("a", "b", "c", "d", "e")
我还有一个名为 df
的表,其中包含一个列,其中包含值,如 a_1
, b_2
, c_3
, 等等。
我该如何过滤 letters
,只保留在 df
的列中可用的字符。
例如,letters <- c("a", "b", "c", "d", "e", "f")
,df$column
的值为 "a_1"
, "d_3"
, "b_7"
。letters
现在应该变成 c("a", "b", "d")
。
英文:
Let's say I have a vector
letters <- c("a", "b", "c", "d", "e")
I also have a table named df
which has a column containing values such as "a_1", "b_2", c_3"
, ...etc.
How do I filter letters
to only keep characters that are available in the column from df
.
For example, letters <- c("a", "b", "c", "d", "e", "f")
and df$column
values are "a_1", "d_3", "b_7"
. letters
should now become c("a", "b", "d")
.
答案1
得分: 1
我们可以从列中移除_
和一个或多个数字(\\d+
),使用%in%
创建一个逻辑向量并对letters
对象进行子集操作。
letters[letters %in% sub("_\\d+", "", df$column)]
[1] "a" "b" "d"
数据
df <- data.frame(column = c("a_1", "d_3", "b_7"))
英文:
We may remove the _
and one or more digits (\\d+
) from the column, use %in%
to create a logical vector and subset the letters
object
letters[letters %in% sub("_\\d+", "", df$column)]
[1] "a" "b" "d"
data
df <- data.frame(column = c("a_1", "d_3", "b_7"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论