我试图根据另外两个计算的分类变量(包含“是”或“否”类别)为一个组分配一个分类变量(是或否)。如果一行对前两个计算的变量都有“是”,我希望整个组被分配为“是”。必须有更好的方法来做到这一点与过滤器或一些窗口排序功能。下面是我到目前为止所做的那些混乱的代码。 filteredDF
是我希望得到的输出。谢谢!使用dplyr将分类变量分配给组的更好方法?
#install.packages('nycflights13', 'dplyr')
library('nycflights13')
library('dplyr')
data(flights)
filteredDF <- flights %>%
mutate(variable1 = ifelse(month %in% c(1:6) & day %in% c(16:28), yes = 'yes', no = 'no')) %>% #create first calculated categorical variable
mutate(variable2 = ifelse(month %in% c(7:12, 6) & day %in% c(1:16) , yes = 'yes', no = 'no')) %>% #create second calculated categorical variable
group_by(tailnum) %>% # assign groups I'm interested in
mutate(varTogether = ifelse('yes' %in% variable1 & 'yes' %in% variable2, yes = 'yes', no = 'no')) %>% # create 3rd categorical to filter by (assigned by group)
ungroup() %>%
filter(varTogether == 'yes') # filter out what I want
@DirtySockSniffer谢谢,我会补充说,以 –
你不需要做到这一点的问题。你可以在加载包装后输入'flights',但没关系。 –