英文:
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',而且没有任何值:
df2 <- df1 %>% select(c('Interaction.ID','Name.ID','Created.At','Interaction_Type'))
%>% 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:
df2 <- df1 %>% select (c('Interaction.ID','Name.ID','Created.At','Interaction_Type'))
%>% filter ('Interaction_Type' == "print")
Should I use the match()
function instead?
答案1
得分: 1
问题在于filter()
函数期望的是一个变量名(不带引号),而不是一个字符串。去掉围绕"Interaction"的引号将解决这个问题。考虑以下示例:
library(dplyr)
mtcars %>% filter("cyl" == 4)
#> [1] mpg cyl disp hp drat wt qsec vs am gear carb
#> <0 rows> (or 0-length row.names)
在上面的示例中,没有返回任何结果,因为没有实例满足字符串"cyl"
等于数字4。在下面的代码中,filter尝试找到变量cyl
等于4的情况 - 这是期望的结果。
mtcars %>% filter(cyl == 4)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
#> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
#> Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
#> Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
#> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
#> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
#> 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:
library(dplyr)
mtcars %>% filter("cyl" == 4)
#> [1] mpg cyl disp hp drat wt qsec vs am gear carb
#> <0 rows> (or 0-length row.names)
In the example above, no results are returned because there is no instance where the string "cyl"
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.
mtcars %>% filter(cyl == 4)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
#> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
#> Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
#> Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
#> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
#> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
#> 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论