我在一个按性别区分学校的区域中有一个数据集,我正在考虑比较同一所学校内的性别表现,但为此,我想限制我的数据只包括同时教授两种性别的学校。换句话说,我想删除只教授女性或男性的学校。
下面是我当前的代码,但是尽管其中包括几所教授两种性别的学校,但它却给了我零的观察力:
import { Injectable } from '@angular/core';
// import 'rxjs/Rx';
import { HttpClient, HttpHeaders, HttpRequest, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class HttpService {
public currentError: any;
constructor(private http: HttpClient) {
}
get(url): Observable<any> {
return this.http.get(url);
}
post(url, dataarr): Observable<any> {
const body = JSON.stringify(dataarr);
console.log(body);
return this.http.post(url, body);
}
patch(url, dataarr): Observable<any> {
const body = JSON.stringify(dataarr);
console.log(body);
return this.http.patch(url, body);
}
put(url, dataarr): Observable<any> {
const body = JSON.stringify(dataarr);
console.log(url);
console.log(body);
return this.http.put(url, body);
}
logout(url, dataarr): Observable<any> {
const body = JSON.stringify(dataarr);
let headers = new HttpHeaders({
'Login-Type': 'application/json'
});
return this.http.post(url, body, {headers: headers});
}
}
我的问题是,是否有一种有效的方法可以对我的数据进行子集化,而不必手动指定每个教授性别的学校名称?
答案 0 :(得分:5)
当您为filter
赋予多个条件时,会将它们与“和”组合。因此,您的代码查找的学校名称为空白(school_name == ""
)的行,性别为“男”,性别为“女”。
相反,您应该group_by(school_name)
并从那里继续。几个选项:
two_gender_schools_a = riyadh_schools %>%
group_by(school_name) %>%
filter("female" %in% gender & "male" %in% gender)
# %in% checks anywhere in the group, not row by row
two_gender_schools_b = riyadh_schools %>%
group_by(school_name) %>%
filter(n_distinct(gender) > 1)
# look for schools that have more than 1 distinct value for gender