我正在使用一个包含500列和20000行的数据库,我想通过统计模式更改NA数据,因此我避免删除这些值,只需通过特定列的模式进行更改,因此我得到了一个示例库来显示我正在运行的代码

library(tidyverse)
temp <- c(20.37, 18.56, NA, 21.96, 29.53, 28.16,
          36.38, 36.62, 40.03, 27.59, 22.15, 19.85)
humedad <- c(88, 86, 81, 79, 80, 78,
             71, NA, 78, 82, 85, 83)
precipitaciones <- c(72, 33.9, 37.5, 36.6, 31.0, 16.6,
                     1.2, 6.8, 36.8, 30.8, 38.5, 22.7)
precipitaciones2 <- c(72,NA, 6.8, 36.6, 31.0, 16.6,
                      1.2, 6.8, 36.8, 6.8, 38.5, 22.7)
precipitaciones3 <- c(72,NA, 37.5, 36, 2, 16.6,
                      1.2, 8, 0.8, NA, 38.5, 8)
mes <- c("enero", "febrero", "marzo", "abril", "mayo", "junio",
         "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre")

datos <- data.frame(mes = mes, temperatura = temp, humedad = humedad,
                    precipitaciones = precipitaciones,
                    precipitaciones2 = precipitaciones2,
                    precipitaciones3 = precipitaciones3)

我想用更 Big Data 库的统计模式替换NA数据,因此需要为任何其他数据库编程,我有以下代码:

#mode
mode=getmoda<-function(v){
  uniqv<-unique(v)
  uniqv[which.max(tabulate(match(v,uniqv)))]
}


reemplazar<-function(y){
  i=2
  lista_vacia1 <- list()
  lista_vacia2<-list()
  a<-""
  while(i<=5){
    lista_vacia1<-y[,i]                                  #select the column to filter
    lista_vacia2<-lista_vacia1[!is.na(lista_vacia1)]     #remove the NA data
    a<-mode(lista_vacia2)                                #get the mode of the column
    y<-y %>% mutate_at(i,~replace_na(.,a))
    
    a<- ""
    lista_vacia1 <- list()
    lista_vacia2<-list()
    
  }
}

所发生的是,当我运行程序时,它会生成一个无限循环,它不会超出加载范围,也不会显示任何消息.我希望您能帮助我了解为什么会发生这种情况,或者是否有可能更改代码.

推荐答案

在任何函数中,应使用return()指定输出.此外,您的数据框有六列,我不知道您为什么指定while(i<=5).应该是while(i<=6).每次迭代后,i应增加1.

reemplazar<-function(y){
  i=2
  lista_vacia1 <- list()
  lista_vacia2<-list()
  a<-""
  while(i<=6){
    lista_vacia1<-y[,i]                                  #select the column to filter
    lista_vacia2<-lista_vacia1[!is.na(lista_vacia1)]     #remove the NA data
    a<-mode(lista_vacia2)                                #get the mode of the column
    y<-y %>% mutate_at(i,~replace_na(.,a))
    
    a<- ""
    lista_vacia1 <- list()
    lista_vacia2<-list()
    i <- i+1          # increment of i
  }
  return(y)            # Specifying the output object
}

输出为

> reemplazar(datos)
          mes temperatura humedad precipitaciones precipitaciones2 precipitaciones3
1       enero       20.37      88            72.0             72.0             72.0
2     febrero       18.56      86            33.9              6.8              8.0
3       marzo       20.37      81            37.5              6.8             37.5
4       abril       21.96      79            36.6             36.6             36.0
5        mayo       29.53      80            31.0             31.0              2.0
6       junio       28.16      78            16.6             16.6             16.6
7       julio       36.38      71             1.2              1.2              1.2
8      agosto       36.62      78             6.8              6.8              8.0
9  septiembre       40.03      78            36.8             36.8              0.8
10    octubre       27.59      82            30.8              6.8              8.0
11  noviembre       22.15      85            38.5             38.5             38.5
12  diciembre       19.85      83            22.7             22.7              8.0

R相关问答推荐

如何使下一个按钮只出现在Rshiny 的一段时间后?""

当月份额减go 当月份额

使用geom_segment()对y轴排序

计算时间段的ECDF(R)

ComplexHEAT:使用COLUMN_SPLIT时忽略COLUMN_ORDER

您是否可以将组添加到堆叠的柱状图

R中的类别比较

扩展R中包含列表的数据框

将列表中的字符串粘贴到R中for循环内的dplyr筛选器中

按两个因素将观测值分组后计算单独的百分比

如何将EC50值绘制在R中的剂量-react 曲线上?

基于R中的引用将向量值替换为数据框列的值

如何使用ggsurvfit包更改风险表中的标签名称?

对一个列表中另一个列表中的元素进行计数

删除列表中的奇数长度元素

R:统计行值在其他行值范围内的实例

如何计算对偶范数?

根据R中另一个表中的变量,删除符合特定条件的数据集中的随机记录

如何行绑定存储在多级嵌套列表中的数据帧,并为每一级添加标识符列?

根据引用在数据框中添加列