2017-05-04 64 views
0
library(dplyr) devel version, soon-to-be released 0.6 
library(tidyr) 

下面是一個簡單的數據集。 Q1Sat-Q3Sat變量是滿意度水平,Q1Used-Q3Used變量指的是調查對象是否使用了他們所評定的項目。問卷在調查中一起提出。實際上,真實數據集至少包含50個Sat變量和Used變量。使用Devel版本的Dplyr範圍過濾器進行條件過濾

Q1Sat<-c("Neutral","Neutral","VSat","Sat","Neutral","Sat","VDis","Sat","Sat","VSat") 
Q2Sat<-c("Neutral","VSat","Dis","Dis","VDis","Sat","Sat","VSat","Neutral","Dis") 
Q3Sat<-c("Sat","Sat","Diss","Neutral","VSat","VDis","Sat","Sat","Sat","Neutral") 
Q3Used<-c("Yes","No","Yes","Yes","Yes","Yes","Yes","Yes","Yes","No") 
Q2Used<-c("Yes","Yes","Yes","Yes","No","No","Yes","Yes","Yes","Yes") 
Q1Used<-c("Yes","Yes","Yes","No","No","Yes","Yes","Yes","No","Yes") 
House<-c("Yes","No","Unsure","Yes","Yes","No","Unsure","Unsure","Yes","Yes") 

Test<-data_frame(Q1Sat,Q2Sat,Q3Sat,Q1Used,Q2Used,Q3Used,House) 

我想用下面的代碼將數據重新組織到一個百分比表中。但是,我需要過濾q1Used - q3Used變量以僅包含「是」,而House變量僅包含「是」。正如已經提到的那樣,q1Sat與q1Used綁定,所以q1Sat應該只包含在q1Used爲「是,而House變量爲」是「的情況下,我需要對q2Sat和q3Sat執行此操作。我嘗試使用dplyr的devel版本的範圍過濾器,但我不知道如何使用它與多個變量 - q1Used:q3Used,以及衆議院

那麼,我將如何添加過濾器房子!=「是」在下面的代碼的作用域過濾器?不devel的版本

Test%>% 
filter_at(vars(Q1Used:Q35Used),all_vars(. != 1)%>% 
select(Q1Sat:Q3Sat)%>% 
gather()%>% 
count(key,value)%>% 
mutate(perc=round(n/sum(n),2))%>% 
select(-n)%>% 
spread(value,perc) 
+0

如果您只選擇了'星期六'變量,您如何獲得'過濾器''已使用'變量?此外,根據您的情況('q1Used - q3使用變量只包含「是」,而House變量只包含「否」'),過濾後將有0行,因爲沒有行滿足條件 – akrun

+0

我想我應該在select中包含「Used」變量,然後......這也是問題的一部分,我只是希望找到一種更簡單的方法來使用管道和tidyverse編寫上面的代碼。至於沒有滿足條件的行,我將「House」變量從no更改爲yes。這真的沒關係,學習如何在不同類型的變量上共同使用作用域過濾器... – Mike

+0

我編輯了代碼......它現在應該會更好嗎? – Mike

回答

0

解決方案。總的想法是,我們在不需要重新編碼值NA代替過濾。

sat = Test %>% select(Q1Sat:Q3Sat, House) %>% 
     gather(key_sat, Sat, -House) 
used = Test %>% select(Q1Used:Q3Used) %>% 
    gather(key_used, Used) 

cbind(used, sat) %>% 
    group_by(key_sat) %>% 
    mutate(
     value = ((Used != "No") & (House == "Yes")) * 1, 
     base = sum(value) 
    ) %>% 
    group_by(key_sat, Sat) %>% 
    summarise(perc = sum(value)/sum(base[1])) %>% 
    spread(Sat,perc)