我正在尝试使用dplyr过滤()数据帧,我想基于另一个数据框中的列表/值指定的自定义范围进行过滤。
我想要做的一个例子如下。任何帮助将不胜感激。
package week10;
import java.util.*;
/**
* Skeleton of the recursive implementation of a general tree.
*
* @author Michael Albert
* @param <T> The type of values stored in the tree.
*/
public class Tree<T> {
private T rootValue;
private List<Tree<T>> children;
public Tree(T rootValue, List<Tree<T>> children) {
this.rootValue = rootValue;
this.children = children;
}
public Tree(T rootValue) {
this(rootValue, new ArrayList<Tree<T>>());
}
public int size() {
int count = 1;
for (Tree<T> child : children) {
count += child.size();
}
return count;
}
public int maxDegree() {
// Get the number of immediate children
int numChildren = this.children.size();
// Find the max of all children
int maxOfChildren = 0;
for (Tree<T> child : children) {
maxOfChildren = Math.max(maxOfChildren, child.maxDegree());
}
// return the greater of immediate child or max of children
return Math.max(numChildren, maxOfChildren);
}
public void add(Tree<T> child) {
children.add(child);
}
public Tree<T> find(T value) {
if (rootValue.equals(value)) {
return this;
}
for (Tree<T> child : children) {
Tree<T> match = child.find(value);
if (match != null) {
return match;
}
}
return null;
}
public List<T> postOrder() {
ArrayList<T> list = new ArrayList<T>();
for (Tree<T> child : children) {
if (!child.children.isEmpty()) {
child.postOrder();
} else {
//list.add(child);
System.out.println(child);
}
}
return list;
}
public String toString() {
if (children.isEmpty()) {
return rootValue.toString();
}
return rootValue.toString() + ' ' + children.toString();
}
public String toIndentedString() {
// implement this method
return "Not implemented yet!";
}
/** A helper method for testing (used by main). Searches tree for
* the given target and adds white space separated children to
* the tree matching target if there is one.
*
* @param target the root value to seach for.
* @param children a white space separated list of children to add
* to the tree whose value matches target.
*/
private static void addChildren(String target, String children) {
Tree<String> parent = tree.find(target);
if (parent != null) {
for (String child : children.split(" ")) {
parent.add(new Tree<>(child));
}
}
}
/** A tree instance used for testing. */
private static Tree<String> tree;
/**
* Entry point of the program (used for testing).
*
* @param args command line arguments are not used.
*/
public static void main(String[] args) {
System.out.println("Creating tree\n-------------");
tree = new Tree<>("food");
System.out.print(tree + "\nsize: " + tree.size());
System.out.println(", max degree: " + tree.maxDegree());
System.out.println("\nAdding children\n----------------");
addChildren("food", "meat fruit vegetable");
System.out.print(tree + "\nsize: " + tree.size());
System.out.println(", max degree: " + tree.maxDegree());
System.out.println("\nAdding deeper children\n----------------------");
addChildren("meat", "chicken beef fish");
addChildren("fish", "salmon cod tuna shark");
addChildren("vegetable", "cabbage");
System.out.print(tree + "\nsize: " + tree.size());
System.out.println(", max degree: " + tree.maxDegree());
System.out.println("\nPostorder\n---------");
System.out.println(tree.postOrder());
System.out.println("\nIndented string\n---------------");
System.out.print(tree.toIndentedString());
}
}
还有另一种方法吗?或者使用现有代码执行此操作?在dplyr包中是否有我遗漏的东西。
非常感谢你。
答案 0 :(得分:0)
我认为问题是between
期望边界条件的单个值。
您可以尝试:
c<- b %>% filter(source > al & source < ah)
但是,我发现这有点奇怪,因为al
和ah
将被回收以匹配b$source
的大小。
答案 1 :(得分:0)
between
期望下限/上限的单个值。你可以循环遍历这样的值:
for(i in 1:length(al)){
print(b %>% filter(between(source,al[i],ah[i])))
}
答案 2 :(得分:0)
您可以在dplyr中使用rowwise()
执行此操作:
b %>% rowwise() %>% filter(sum((source>al)*(source<ah))>0) %>% ungroup()
返回:
source
<int>
1 1
2 5
3 22
4 36
5 41
sum((source>al)*(source<ah))
返回值下降的间隔数。如果它落在一个区间内,它将返回1.如果它落在2(如果重叠间隔),它将返回2等。
您还可以使用findInterval
方法。你可以从al和ah创建一个向量:
v=c(rbind(sort(al),ah[order(al)]))
[1] 0.9 1.1 4.9 5.1 21.9 22.1 35.9 36.1 40.9 41.1
然后查明您的值是否落入奇数或偶数间隔:
b %>% filter(findInterval(source,v)%%2==1)
请注意,此方法仅在您的间隔不重叠时才有效。