提迪尔的collect()和unite()

时间:2018-08-11 15:54:58

标签: r tidyr likert

我使用tidyr的collect()和unite()函数碰到了墙。

此示例是预期的输出

# sample Data
> wide_df 
    col A   B  C 
1   X   1   2  3 
2   Y   4   5  6

> gather(wide_df, my_key, my_val, -col)
  col my_key my_val
1  X   A      1 
2  Y   A      4 
3  X   B      2 
4  Y   B      5 
5  X   C      3 
6  Y   C      6

但是,使用我的实际数据,我得到了不同的结果。

# Actual Data
>Parents_Pulse_Survey
    col         Too_demanding       Cost_Too_High   Prefer_Stay_ParentHome
1   Austin     NA                   NA             Prefer_Stay_ParentHome
2   Austin     Too_demanding        NA                 NA

reasons <-gather(Austin_Parent_Pulse_Survey, reasonsWhy,High_Childcare_Cost:Other_Stay_At_Home)

然后我得到这个输出

reasons
# A tibble: 30,900 x 2
   reasonsWhy `High_Childcare_Cost:Other_Stay_At_Home`
   <chr>      <chr>                                   
 1 Austin     Yes                                     
 2 Austin     Yes                                     
 3 Austin     Yes                                     
 4 Austin     Yes                                     
 5 Austin     Yes                                     
 6 Austin     Yes  

我在做什么错了?

我希望我的实际输出看起来像样本输出。 非常感谢您的帮助。

我想获得这种类型的输出

# Intended Output
reasons         
   Respondent      Reasons                                   
 1 Austin          High_Childcare_Cost                                    
 2 Austin          Other_Stay_At_Home                                    
 3 Austin          Too_demanding                                     
 4 Austin          Too_demanding                                     
 5 Austin         High_Childcare_Cost                                      
 6 Austin         Other_Stay_At_Home 

1 个答案:

答案 0 :(得分:0)

您必须输入调用方式(数据库,attribute_name,value_variable_name,所收集的列),好像您错过了为value_variable_name命名一样。以下是基于虹膜的示例

 str(iris)
 reasons <- gather(iris, 
                   reasonsWhy, Value,
                   Sepal.Length:Petal.Width)
相关问题