根据列中的数值筛选向量。

huangapple go评论61阅读模式
英文:

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 &lt;- c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;)

I also have a table named df which has a column containing values such as &quot;a_1&quot;, &quot;b_2&quot;, c_3&quot;, ...etc.

How do I filter letters to only keep characters that are available in the column from df.

For example, letters &lt;- c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;, &quot;f&quot;) and df$column values are &quot;a_1&quot;, &quot;d_3&quot;, &quot;b_7&quot;. letters should now become c(&quot;a&quot;, &quot;b&quot;, &quot;d&quot;).

答案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(&quot;_\\d+&quot;, &quot;&quot;, df$column)]
[1] &quot;a&quot; &quot;b&quot; &quot;d&quot;

data

df &lt;- data.frame(column = c(&quot;a_1&quot;, &quot;d_3&quot;, &quot;b_7&quot;))

huangapple
  • 本文由 发表于 2023年2月24日 01:58:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548600.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定