如何使用dplyr库选择特定变量,然后将一个变量与特定值匹配。

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

How to use the dplyr library to select specific variables and then match a variable with a specific value

问题

我需要使用dplyr中的filter函数来检索所有变量Interaction_Type等于"Print"的值;然而,下面的代码未从我的数据框df1中检索到任何内容,除了列出变量'Interaction.ID','Name.ID','Created.At',而且没有任何值:

  1. df2 <- df1 %>% select(c('Interaction.ID','Name.ID','Created.At','Interaction_Type'))
  2. %>% filter('Interaction_Type' == "Print")

我应该使用match()函数吗?

英文:

I need to use the filter function in dplyr to retrieve all values in which my variable entitled Interaction_Type is equal to "Print"; however the code below is not retrieving anything from my data frame df1 other than listing the variables with 'Interaction.ID','Name.ID','Created.At' with no values at all:

  1. df2 &lt;- df1 %&gt;% select (c(&#39;Interaction.ID&#39;,&#39;Name.ID&#39;,&#39;Created.At&#39;,&#39;Interaction_Type&#39;))
  2. %&gt;% filter (&#39;Interaction_Type&#39; == &quot;print&quot;)

Should I use the match() function instead?

答案1

得分: 1

问题在于filter()函数期望的是一个变量名(不带引号),而不是一个字符串。去掉围绕"Interaction"的引号将解决这个问题。考虑以下示例:

  1. library(dplyr)
  2. mtcars %>% filter("cyl" == 4)
  3. #&gt; [1] mpg cyl disp hp drat wt qsec vs am gear carb
  4. #&gt; &lt;0 rows&gt; (or 0-length row.names)

在上面的示例中,没有返回任何结果,因为没有实例满足字符串"cyl"等于数字4。在下面的代码中,filter尝试找到变量cyl等于4的情况 - 这是期望的结果。

  1. mtcars %>% filter(cyl == 4)
  2. #&gt; mpg cyl disp hp drat wt qsec vs am gear carb
  3. #&gt; Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
  4. #&gt; Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
  5. #&gt; Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
  6. #&gt; Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
  7. #&gt; Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
  8. #&gt; Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
  9. #&gt; Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
  10. #&gt; Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
  11. #&gt; Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
  12. #&gt; Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
  13. #&gt; Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2

<sup>创建于2023年05月28日,使用reprex v2.0.2</sup>。

英文:

The problem is that the filter() function is expecting a variable name (without quotes) rather than a string. Removing the quotes from around Interaction will solve the problem. Consider the following:

  1. library(dplyr)
  2. mtcars %&gt;% filter(&quot;cyl&quot; == 4)
  3. #&gt; [1] mpg cyl disp hp drat wt qsec vs am gear carb
  4. #&gt; &lt;0 rows&gt; (or 0-length row.names)

In the example above, no results are returned because there is no instance where the string &quot;cyl&quot; is equals the number 4. In the code below, filter is trying to find the cases where the variable cyl equals 4 - this is the desired result.

  1. mtcars %&gt;% filter(cyl == 4)
  2. #&gt; mpg cyl disp hp drat wt qsec vs am gear carb
  3. #&gt; Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
  4. #&gt; Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
  5. #&gt; Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
  6. #&gt; Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
  7. #&gt; Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
  8. #&gt; Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
  9. #&gt; Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
  10. #&gt; Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
  11. #&gt; Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
  12. #&gt; Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
  13. #&gt; Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2

<sup>Created on 2023-05-28 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月29日 01:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352817.html
匿名

发表评论

匿名网友

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

确定