没有变量数据时添加NULL

时间:2019-02-20 20:03:12

标签: r dataframe tidyverse missing-data

下面是一个示例DF,它说明了我遇到的问题。我遇到一个问题,该组没有所有变量的值,因此R不会为此返回任何内容。也就是说,在R下方的数据中返回:

Course   Gender  n
English1 Female  1
English1 Male    3
English2 Female  2
English2 Male    1
English2 Unknown 1
English3 Female  3
English3 Unknown 1

df1 <- data.frame("Course"=c("English1", "English1", "English1", "English1", 
                             "English2", "English2", "English2", "English2", 
                             "English3", "English3", "English3", "English3"),  
                  Gender=c("Male", "Female", "Male", "Male", "Male", "Female", 
                           "Unknown", "Female", "Female", "Female", "Female", 
                           "Unknown"),  Grade=c("A", "A", "C", "D", "D", "A", "B", 
                                                "C", "B", "D", "A", "C"))
library(dplyr)
df1 %>% group_by(Course, Gender) %>% count

我想做的是在课程组内没有性别计数时返回Null或0。我希望数据返回此值(我用*标记了新行):

Course   Gender  n
English1 Female  1
English1 Male    3
English1 Unknown 0*
English2 Female  2
English2 Male    1
English2 Unknown 1
English3 Female  3
English3 Male    0*
English3 Unknown 1

我之所以需要这样做,是因为我需要一个rMarkdown输出具有相同的组(每个课程三个性别)。任何帮助将不胜感激

3 个答案:

答案 0 :(得分:2)

data.frame(xtabs(a~Gender+Course,cbind(a=1,df1)))[c(2,1,3)]
    Course  Gender Freq
1 English1  Female    1
2 English1    Male    3
3 English1 Unknown    0
4 English2  Female    2
5 English2    Male    1
6 English2 Unknown    1
7 English3  Female    3
8 English3    Male    0
9 English3 Unknown    1

如果您不关心订购,那么:

data.frame(xtabs(Grade~.,cbind(Grade=1,df1)))

答案 1 :(得分:1)

实际上,在您的代码中,在DECLARE @sqlCommand VARCHAR(3000); DECLARE @tableList TABLE(Value NVARCHAR(128)); DECLARE @TableName VARCHAR(128); DECLARE @RecordCount INT; -- get a cursor with a list of table names and their record counts DECLARE MyCursor CURSOR FAST_FORWARD FOR SELECT t.name TableName, i.rows Records FROM sysobjects t, sysindexes i WHERE t.xtype = 'U' -- only User tables AND i.id = t.id AND i.indid IN(0, 1) -- 0=Heap, 1=Clustered Index AND i.rows < 10 -- Filter by number of records in the table AND t.name LIKE 'Test_%'; -- Filter tables by name. You could also provide a list: -- AND t.name IN ('MyTable1', 'MyTable2', 'MyTable3'); -- or a list of tables to exclude: -- AND t.name NOT IN ('MySpecialTable', ... ); OPEN MyCursor; FETCH NEXT FROM MyCursor INTO @TableName, @RecordCount; -- for each table name in the cursor, delete all records from that table: WHILE @@FETCH_STATUS = 0 BEGIN SET @sqlCommand = 'DELETE FROM ' + @TableName; EXEC (@sqlCommand); FETCH NEXT FROM MyCursor INTO @TableName, @RecordCount; END; CLOSE MyCursor; DEALLOCATE MyCursor; 函数之后使用 Column 1 Column 2 Column 3 ---------------------------------------- Honda Civic 500 Civic Honda 250 Alfa Romeo 1650000 Alfa Romeo 150000 Renault Broadway 10 Aston Martin 750 Renault Megane 2000 Megane Renault 150 Ferrari Enzo 88500 Ferrari Enzo 500 Renault Megane 30000 函数的 Column 1 Column 2 Column 3 --------------------------------------- Honda Civic 750 Alfa Romeo 1800000 Renault Broadway 10 Aston Martin 750 Renault Megane 32150 Ferrari Enzo 89000 解决方案has already been solved here。您可以选择fill = list(value = 0)选项,以使用所需的值填充那些缺失的行,但也可以是其他任何值。

请注意,您必须先dplyr,否则每个组将执行一次此操作,从而复制行。

现在这非常简单,并且已根据您表达需求的方式进行了调整:

complete

答案 2 :(得分:0)

dplyr 0.8.0开始,您只需将.drop = FALSE添加到语句中即可:

df1 %>% 
  group_by(Course, Gender, .drop = FALSE) %>% 
  count

输出:

# A tibble: 9 x 3
# Groups:   Course, Gender [9]
  Course   Gender      n
  <fct>    <fct>   <int>
1 English1 Female      1
2 English1 Male        3
3 English1 Unknown     0
4 English2 Female      2
5 English2 Male        1
6 English2 Unknown     1
7 English3 Female      3
8 English3 Male        0
9 English3 Unknown     1

请注意,如果您仅使用count,则可以简化此操作,并且仍然可以使用:

df1 %>% count(Course, Gender, .drop = FALSE)

# A tibble: 9 x 3
  Course   Gender      n
  <fct>    <fct>   <int>
1 English1 Female      1
2 English1 Male        3
3 English1 Unknown     0
4 English2 Female      2
5 English2 Male        1
6 English2 Unknown     1
7 English3 Female      3
8 English3 Male        0
9 English3 Unknown     1