I'm trying to create a function has work like INDEX in Excel using select and filter in function.
Here is my code but it seem like not working when I tested with one data frame.
index_matching <- function(data = NULL, row_matching, col_matching){
if(!missing(data)) {
#Variable create
col <- quo(col_matching)
row <- row_matching
matching <- data %>%
as.data.frame() %>%
filter(!!col == row) #This row has problem
select(!!col) %>%
return(matching)
} else {
message("You must specify data frame to matching!")
}
}
I want to search the row "B" and column length to have needed data, in this example is 4.9.
reprex::reprex({
raw_input_data <- data.frame(
length = c(5.1, 4.9, 4.7, 4.6, 5),
width = c(3.5, 3, 3.2, 3.1, 3.6)
text = c("A", "B", "C", "D", "E")
)
})
But when I use the function, there has been a error that they return data frame with 0 rows and
reprex::reprex({
index_matching(raw_input_data, row_matching = "B", col_matching = "length")
})
Here is the error
[1] component_type
<0 rows> (or 0-length row.names)
I think it's a issue but I want to ask in R community first. Glad to hear comment.
What exactly is not working? From the code you've provided it's impossible to say.
To help us help you, could you please prepare a repr oducible ex ample (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
1 Like
Many thanks for the guidance. I'm a newbie and this suggest help me a lot. I fixed the question more clear. Thanks
If you want to pass the variable name in col_matching
as a string (meaning in quotes), you'll need to use sym()
in the function.
That would look like:
index_matching <- function(data = NULL, row_matching, col_matching){
if(!missing(data)) {
#Variable create
col <- sym(col_matching)
row <- row_matching
matching <- data %>%
as.data.frame() %>%
filter(!!col == row) #This row has problem
select(matching, !!col)
} else {
message("You must specify data frame to matching!")
}
}
index_matching(raw_input_data, row_matching = "B", col_matching = "text")
text
1 B
You will still get 0-row output if you use a column that doesn't contain "B", though.
If you want to pass the column without quotes you need enquo()
instead of quo()
in your function.
index_matching <- function(data = NULL, row_matching, col_matching){
if(!missing(data)) {
#Variable create
col <- enquo(col_matching)
row <- row_matching
matching <- data %>%
as.data.frame() %>%
filter(!!col == row) #This row has problem
select(matching, !!col)
} else {
message("You must specify data frame to matching!")
}
}
index_matching(raw_input_data, row_matching = "B", col_matching = text)
text
1 B
4 Likes
system
Closed
June 24, 2019, 4:51pm
5
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.