library("tidyverse")
demo1 <- as.data.frame(tribble(
~cut, ~freq, ~distance,
"Fair", 1610, 4,
"Good", 4906, 100,
"Very Good", 12082, 45,
"Premium", 13791, 50,
"Ideal", 21551, 34,
"Very good", 14938, 60
))
demo2 <- as.data.frame(tribble(
~cut, ~freq, ~distance,
"Very Good", 403, 14,
"Fair", 3920, 25,
"Premium", 5938, 5,
"Good", 4593, 40,
"Ideal", 21551, 2.4,
))
For each observation (row) in the demo1 data, I would like to find nearest three in terms of distance related to the row in demo2 data. The result needs to be combined into a same .csv file.
My code implemented shows errors.
Result<-data.frame()
for (k in 1:nrow(demo1)) {
Result <- demo2 %>%
filter(abs(demo2$distance-demo1$distance[k])????)
print(Result)
}
Can anyone please help me with this, thanks!