数据框中分类变量的模式插补

问题描述

我有一个只有分类变量的数据框(cat_df)。我想将模式值归因于每个变量中的缺失值。

我尝试了以下代码。但它不起作用。

方式-1

cat_df[is.na(cat_df)] <- modefunc(cat_df,na.rm = TRUE)
cat_df

modefunc <- function(x){
  tabresult <- tabulate(x)
  themode <- which(tabresult == max(tabresult))
  if(sum(tabresult == max(tabresult))>1) themode <- NA
  return(themode)
}

modefunc(cat_df,na.rm = TRUE) 中的错误: 未使用的参数 (na.rm = TRUE)

方式-2

cat_df[is.na(cat_df)] <- my_mode(cat_df[!is.na(cat_df)])
cat_df

my_mode <- function(x){
  unique_x <- unique(x)
  mode <- unique_x[which.max(tabulate(match(x,unique_x)))]
  mode
}

上面的代码不是没有输入模式值

是否有其他方法可以将模式值归入数据框中的分类变量

解决方法

更新:

  • 此模式函数用于数据帧:
my_mode <- function (x,na.rm) {
  xtab <- table(x)
  xmode <- names(which(xtab == max(xtab)))
  if (length(xmode) > 1) xmode <- ">1 mode"
  return(xmode)
}

for (var in 1:ncol(cat_df)) {
  if (class(cat_df[,var])=="numeric") {
    cat_df[is.na(cat_df[,var]),var] <- mean(cat_df[,var],na.rm = TRUE)
  } else if (class(cat_df[,var]) %in% c("character","factor")) {
    cat_df[is.na(cat_df[,var] <- my_mode(cat_df[,na.rm = TRUE)
  }
}

这个模式函数是针对向量的 试试这个,请告诉我。

#define missing values in vector
values <- unique(cat_column)[!is.na(cat_column)]
# mode of cat_column
themode <- values[which.max(tabulate(match(cat_column,values)))] 
#assign missing vector
imputevector <- cat_column                                  
imputevector[is.na(imputevector)] <- themode
,

用户自定义函数

这是我使用的模式函数,在实际存在多种模式的情况下,我使用附加行来选择一种模式:

include 'emu8086.inc' 
.model small
.stack 100h

.data
n_line db 0ah,0dh,"$"  ;for new line
msg1 db "input : $" 
msg2 db 0ah,"Outpur : $"
msg3 db 0ah,"invalid input given. $"  
i db ?

.code
main proc
    mov ax,@data
    mov ds,ax

@infinite:  
    mov i,0d           ;for infinite process i=0 at initialization
         
    lea dx,msg1   
    mov ah,09
    int 21h 

                       ;clear bx register
    xor bx,bx          ;XOR operation with self will always produce 0    
        
    
    mov ah,1
    @input:            
        int 21h        
        
        cmp al,50      ;check for input only 0 and 1
        jge @invalid
        cmp al,48
        jl @invalid
                       ;65d = 01000001 
        and al,0Fh     ;0Fh = 00001111
                       ;AND = 01000001
        shl bx,1
        or bl,al       ;store al data at LSB of BL register 
                 
        inc i
        cmp i,8d
        jl @input
        jge @next_step
    
    @next_step:   
        lea dx,msg2   
        mov ah,9
        int 21h
         
        mov dl,bl       ;print Ascii value
        mov ah,2
        int 21h  
        
        lea dx,n_line   ;for new line
        mov ah,9
        int 21h  
        
        jmp @infinite
            
         
@stop:       
    mov ah,4ch
    int 21h
    main endp
@invalid:
    lea dx,msg3
    mov ah,9
    int 21h
    jmp @stop
end main

DescTools::Mode()

但是其他人已经编写了模式函数。一种可能性是在 my_mode <- function(x) { ux <- unique(x) tab <- tabulate(match(x,ux)) mode <- ux[tab == max(tab)] ifelse(length(mode) > 1,sample(mode,1),mode) } # single mode cat_col_1 <- c(1,1,2,NA) cat_col_1 #> [1] 1 1 2 NA cat_col_1[is.na(cat_col_1)] <- my_mode(cat_col_1) cat_col_1 #> [1] 1 1 2 1 # random sample among multimodal cat_col_2 <- c(1,NA) cat_col_2 #> [1] 1 1 2 2 NA cat_col_2[is.na(cat_col_2)] <- my_mode(cat_col_2) cat_col_2 #> [1] 1 1 2 2 2 包中,名为 DescTools

因为它在有多个模式的情况下返回多种模式,所以您需要决定在该事件中做什么。

这是一个带有替换的随机抽样示例,替换缺失值所需的模式数。

Mode()

reprex package (v1.0.0) 于 2021 年 4 月 16 日创建