如何创建一个数据框,其中所有序数变量都是列,并且具有特定事件的频率

问题描述 投票:0回答:1

我有一个序数据框,其中包含调查格式的答案。我想将每个因子转换为可能的列,以便按特定事件的频率获取它们。

I have tried lapply, dplyr to get frequencies but failed

as.data.frame(apply(mtfinal, 2, table))

mtfinalf<-mtfinal %>%
group_by(q28) %>%
summarise(freq=n())

data.frame形式的预期结果

Frequency table with respect to q28's factors

Expected Results in the form of data.frame
q28  sex1  sex2  race1  race2 race3 race4 race5 race6 race7 age1 age2   
2     0                                                         
3     0                                                         
4     23                                                            
5     21





    Actual Results
    $age

      1   2   3   4   5   6   7 
      6   2 184 520 507 393 170 

    $sex

       1    2 
    1239  543 

    $grade

      1   2   3   4 
    561 519 425 277 

    $race7

       1    2    3    4    5    6 
     179   21   27  140   17 1307 
       7 
      91 

    $q8

      1   2   3   4   5 
    127 259 356 501 539 

    $q9

      1   2   3   4   5 
    993 224 279  86 200 

$q28

       2    3    4    5 
    1034  533   94  121
r dplyr plyr frequency sapply
1个回答
0
投票

这将为您提供唯一组合数量的计数。你问的是不可能的,因为在性别,种族和年龄之间会有重叠。

mtfinalf<-mtfinal %>%
 group_by(q28,age,race,sex) %>%
 tally() 
© www.soinside.com 2019 - 2024. All rights reserved.