默认情况下的错误(x, nmax = nmax): unique()只适用于R中的向量

时间:2021-12-13 15:51:05

I was trying to address the missing values in R through the following code

我试图通过下面的代码来处理R中缺失的值

  {ds$bmi=ifelse(is.na(ds$bmi), ave(ds$bmi, Fun=function(x) mean(y,na.rm=TRUE)),ds$bmi)}   

it is giving following error

它会产生以下错误

Error in unique.default(x, nmax = nmax) : unique() applies only to vectors

默认值(x, nmax = nmax)中的错误:unique()只适用于向量

Please help how to address this error

请帮忙解决这个错误

1 个解决方案

#1


0  

Here 'ave' doesn't have any of the "Grouping variables, typically factors, all of the same length as x." as described in ?ave (the ... args).

在这里,ave没有任何“分组变量,通常是因子,所有的长度都和x一样”,如在?ave (the…args)。

If the goal is to replace NA's with the mean of bmi, maybe just straight up mean is what you want?

如果我们的目标是用bmi的平均值来代替NA,那么直接的平均值就是你想要的吗?

hm_rows=10;
ds=data.frame(bmi=runif(hm_rows,0,10))
ds[c(1,2,4,6),"bmi"] <- NA
{ds$bmi=ifelse(is.na(ds$bmi), mean(ds$bmi,na.rm=TRUE),ds$bmi)}

#1


0  

Here 'ave' doesn't have any of the "Grouping variables, typically factors, all of the same length as x." as described in ?ave (the ... args).

在这里,ave没有任何“分组变量,通常是因子,所有的长度都和x一样”,如在?ave (the…args)。

If the goal is to replace NA's with the mean of bmi, maybe just straight up mean is what you want?

如果我们的目标是用bmi的平均值来代替NA,那么直接的平均值就是你想要的吗?

hm_rows=10;
ds=data.frame(bmi=runif(hm_rows,0,10))
ds[c(1,2,4,6),"bmi"] <- NA
{ds$bmi=ifelse(is.na(ds$bmi), mean(ds$bmi,na.rm=TRUE),ds$bmi)}