使用 with() 执行交叉制表时的警告?

Warnings when perform cross tabulation using with()?

我正在尝试在 R 中执行交叉制表并计算均值。

首先我选择了变量并创建了一个新的数据框:

      seed <- ruk_trial$Ruk_seed_input   #integer   
      soilec <- ruk_trial$Ruk_soilEC     #num
      ruk_trial$code_smoo[ruk_trial$code_smoo == 0] <- 'US'
      ruk_trial$code_smoo[ruk_trial$code_smoo == 1] <- 'LS'
      ruk_trial$code_smoo[ruk_trial$code_smoo == 2] <- 'HS'
      zones <- ruk_trial$code_smoo       #chr
      netincome <- ruk_trial$NetIncome   #num
      yield <- ruk_trial$Dry_yield       #num
      ruk_df <- as.data.frame(cbind(seed,soilec,zones,netincome,yield))

然后我使用 with():

      with(ruk_df, tapply(netincome, list(zones=zones, seed=seed), mean))

但事实证明:

        seed
  zones 105 120 75 90
     HS  NA  NA NA NA
     LS  NA  NA NA NA
     US  NA  NA NA NA
  There were 12 warnings (use warnings() to see them)

  > warnings()
 Warning messages:
 1: In mean.default(X[[i]], ...) :
  argument is not numeric or logical: returning NA

如果我使用原始数据集,它可以工作:

 > cross.tab<- with(ruk_trial, tapply(netincome, list(zones=zones,seed=seed), mean))

任何人都可以告诉我是什么原因导致警告?

试试这个:

  ruk_df <- data.frame(seed,soilec,zones,netincome,yield, stringsAsFactors=FALSE)